我可以通过以下代码在Swift中使用for in循环
for i in 0..<5
{
print ("Four multiplied by (i) results in (i*4)" )
}
但是如何使用小于">"条件的for呢
for i in 10>..5
{
print ("Four multiplied by (i) results in (i*4)" )
}
显示error: '>'不是后缀一元操作符
使用stride()方法:
for i in 10.stride(to: 5, by: -1) {
print ("Four multiplied by (i) results in (i*4)" )
}
将递增到10、9、8、7和6。如果您希望包含5,请使用through:
代替:
for i in 10.stride(through: 5, by: -1) {
print ("Four multiplied by (i) results in (i*4)" )
}