无法形成范围以<结尾的范围在进行循环之前开始检查范围吗? [英] Can't form Range with end < start Check range before doing for loop?

查看:94
本文介绍了无法形成范围以<结尾的范围在进行循环之前开始检查范围吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我不太了解的快速代码更改.

I'm encountering a change in the swift code which i do not quite understand.

var arr = []
for var i = 1; i <= arr.count; i += 1
{
    print("i want to see the i \(i)")
}

我有一个程序来获取结果数组,该结果数组也可以为空.上面的for循环没有问题. 现在,苹果要我将代码更改为以下代码.但是,如果数组为空,则会崩溃.

I have a program that gets an result array which can also be empty. This is no problem with the for loop above. Now apple wants me to change the code into the following. But this will crash if the array is empty.

var arr = []
for i in 1...arr.count
{
   print("i want to see the i \(i)")
}

在执行类似的循环之前,我真的必须先检查范围吗?

Do I really have to check the range first before i do a loop like?

var arr = []
if (arr.count >= 1){
    for i in 1...arr.count
    {
        print("I want to see the i \(i)")
    }
}

是否有更智能的解决方案?

Is there a smarter solution?

推荐答案

如果只想遍历一个集合,请使用for <element> in <collection>语法.

If you just want to iterate over a collection, then use the for <element> in <collection> syntax.

for element in arr {
    // do something with element
}

如果每次迭代还需要访问元素的索引,则可以使用

If you also need access to the index of the element at each iteration, you can use enumerate(). Because indices are zero based, the index will have the range 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的范围).

You can always add one to the index at each iteration in order to access the position (to get a range of 1..<arr.count+1).

如果这些方法都不能解决您的问题,则可以使用范围0..<arr.count遍历数组的索引,也可以使用

If none of these solve your problem, then you can use the range 0..<arr.count to iterate over the indices of your array, or as @vacawama says, you could use the range 1..<arr.count+1 to iterate over the positions.

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也是一个空范围.

0..<0 cannot crash for an empty array as 0..<0 is just an empty range, and 1..<arr.count+1 cannot crash for an empty array as 1..<1 is also an empty range.

另请参见 @ vacawama在下面的评论,内容涉及使用stride安全地执行更多自定义范围.例如(Swift 2语法):

Also see @vacawama's comment below about using stride for safely doing more custom ranges. For example (Swift 2 syntax):

let startIndex = 4
for i in startIndex.stride(to: arr.count, by: 1) {
    // i = 4, 5, 6, 7 .. arr.count-1
}

Swift 3语法:

Swift 3 syntax:

for i in stride(from: 4, to: arr.count, by: 1) {
    // i = 4, 5, 6, 7 .. arr.count-1
}

这是startIndex是范围开始的数字,arr.count是范围将保持在其下的数字,而1是跨步长度.如果您的数组中的元素少于给定的起始索引,则将永远不会进入循环.

This is where startIndex is the number to start the range at, arr.count is the number that the range will stay below, and 1 is the stride length. If your array has less elements than the given starting index, then the loop will never be entered.

这篇关于无法形成范围以&lt;结尾的范围在进行循环之前开始检查范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆