无法形成 Range with end < start 在执行 for 循环之前检查范围?
Can't form Range with end < start Check range before doing for loop?
我遇到了 swift 代码的变化,我不太明白。
var arr = []
for var i = 1; i <= arr.count; i += 1
{
print("i want to see the i \(i)")
}
我有一个程序可以获取一个结果数组,该数组也可以为空。这对于上面的for循环是没有问题的。
现在苹果要我把代码改成下面这样。但如果数组为空,这将崩溃。
var arr = []
for i in 1...arr.count
{
print("i want to see the i \(i)")
}
我真的必须先检查范围再做循环吗?
var arr = []
if (arr.count >= 1){
for i in 1...arr.count
{
print("I want to see the i \(i)")
}
}
有没有更聪明的解决方案?
这应该产生与第一个示例相同的结果,没有错误...
var arr = []
var i=1
for _ in arr
{
print("i want to see the i \(i)")
i += 1
}
...尽管这似乎是一种计算数组中元素的复杂方法 (arr.count),所以我怀疑这个问题的意义远不止于此。
如果您只想遍历集合,请使用 for <element> in <collection>
语法。
for element in arr {
// do something with element
}
如果你还需要在每次迭代时访问元素的索引,你可以使用enumerate()
。因为索引是从零开始的,所以索引的范围是 0..<arr.count
.
for (index, element) in arr.enumerate() {
// do something with index & element
// if you need the position of the element (1st, 2nd 3rd etc), then do index+1
let position = index+1
}
您始终可以在每次迭代时向索引添加一个以访问该位置(以获得 1..<arr.count+1
的范围)。
如果其中 none 解决了您的问题,那么您可以使用范围 0..<arr.count
来遍历数组的索引,或者作为 ,您可以使用范围1..<arr.count+1
迭代位置。
for index in 0..<arr.count {
// do something with index
}
for position in 1..<arr.count+1 {
// do something with position
}
0..<0
不会因为空数组而崩溃,因为 0..<0
只是一个空范围,1..<arr.count+1
不会因为空数组而崩溃,因为 1..<1
也是空的范围。
另请参阅 关于使用 stride
安全地进行更多自定义范围的信息。例如 (Swift 2 句法):
let startIndex = 4
for i in startIndex.stride(to: arr.count, by: 1) {
// i = 4, 5, 6, 7 .. arr.count-1
}
Swift 3 语法:
for i in stride(from: 4, to: arr.count, by: 1) {
// i = 4, 5, 6, 7 .. arr.count-1
}
这是 startIndex
开始范围的数字,arr.count
是范围将保持在下方的数字,1
是步幅。如果您的数组的元素少于给定的起始索引,则永远不会进入循环。
在这种情况下,显而易见的解决方案是:
var arr = []
for i in arr.indices {
print("I want to see the i \(i)") // 0 ... count - 1
print("I want to see the i \(i + 1)") // 1 ... count
}
但仔细阅读
我遇到了 swift 代码的变化,我不太明白。
var arr = []
for var i = 1; i <= arr.count; i += 1
{
print("i want to see the i \(i)")
}
我有一个程序可以获取一个结果数组,该数组也可以为空。这对于上面的for循环是没有问题的。 现在苹果要我把代码改成下面这样。但如果数组为空,这将崩溃。
var arr = []
for i in 1...arr.count
{
print("i want to see the i \(i)")
}
我真的必须先检查范围再做循环吗?
var arr = []
if (arr.count >= 1){
for i in 1...arr.count
{
print("I want to see the i \(i)")
}
}
有没有更聪明的解决方案?
这应该产生与第一个示例相同的结果,没有错误...
var arr = []
var i=1
for _ in arr
{
print("i want to see the i \(i)")
i += 1
}
...尽管这似乎是一种计算数组中元素的复杂方法 (arr.count),所以我怀疑这个问题的意义远不止于此。
如果您只想遍历集合,请使用 for <element> in <collection>
语法。
for element in arr {
// do something with element
}
如果你还需要在每次迭代时访问元素的索引,你可以使用enumerate()
。因为索引是从零开始的,所以索引的范围是 0..<arr.count
.
for (index, element) in arr.enumerate() {
// do something with index & element
// if you need the position of the element (1st, 2nd 3rd etc), then do index+1
let position = index+1
}
您始终可以在每次迭代时向索引添加一个以访问该位置(以获得 1..<arr.count+1
的范围)。
如果其中 none 解决了您的问题,那么您可以使用范围 0..<arr.count
来遍历数组的索引,或者作为 1..<arr.count+1
迭代位置。
for index in 0..<arr.count {
// do something with index
}
for position in 1..<arr.count+1 {
// do something with position
}
0..<0
不会因为空数组而崩溃,因为 0..<0
只是一个空范围,1..<arr.count+1
不会因为空数组而崩溃,因为 1..<1
也是空的范围。
另请参阅 stride
安全地进行更多自定义范围的信息。例如 (Swift 2 句法):
let startIndex = 4
for i in startIndex.stride(to: arr.count, by: 1) {
// i = 4, 5, 6, 7 .. arr.count-1
}
Swift 3 语法:
for i in stride(from: 4, to: arr.count, by: 1) {
// i = 4, 5, 6, 7 .. arr.count-1
}
这是 startIndex
开始范围的数字,arr.count
是范围将保持在下方的数字,1
是步幅。如果您的数组的元素少于给定的起始索引,则永远不会进入循环。
在这种情况下,显而易见的解决方案是:
var arr = []
for i in arr.indices {
print("I want to see the i \(i)") // 0 ... count - 1
print("I want to see the i \(i + 1)") // 1 ... count
}
但仔细阅读