Swift Fatal error: Index out of range (out of bounds index)

Swift Fatal error: Index out of range (out of bounds index)

你好我正在努力学习swift。我对 javascript 有一点经验,所以我尝试用我通常做的相同方式对这个循环进行建模。该函数实际上输出了它应该输出的内容,但我不断收到一条错误消息,我不确定自己做错了什么。这是我的代码:

import UIKit
let dir: [String] = ["north", "east", "south", "west"]
var num = dir.count
func move(){
    for i in 0 ... num{
        var holder = dir[i]
        switch holder{
        case "north":
            print("you've moved north")
        case "east":
            print("you've moved east")
        case "south":
            print("you've moved south")
        case "west":
            print("you've moved west")
        default:
            print("where you going?")
        }
        if i == 3{
            print("round the world")
        }

    }
}

move()

我在最后一行收到这个错误 "move()"

error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

这是输出到控制台的内容:

you've moved north

you've moved east

you've moved south

you've moved west

round the world

Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444

在 swift 中,有更有效的循环方式...但为了更好地理解您实现的内容...

我已经更新了您的代码...它将 运行 正常。

let dir: [String] = ["north", "east", "south", "west"]
var num = dir.count
func move(){
    for i in 0..<num{
        var holder = dir[i]
        switch holder{
        case "north":
            print("you've moved north")
        case "east":
            print("you've moved east")
        case "south":
            print("you've moved south")
        case "west":
            print("you've moved west")
        default:
            print("where you going?")
        }
        if i == 3{
            print("round the world")
        }

    }
}

move()

输出:-

you've moved north
you've moved east
you've moved south
you've moved west
round the world

Swift 中的快乐编码:-)

import UIKit

class ViewController: UIViewController {

    let dir: [String] = ["north", "east", "south", "west"]

    override func viewDidLoad() {
        super.viewDidLoad()
         move()
        // Do any additional setup after loading the view.
    }

    func move(){
        for (index, element) in dir.enumerated() {
       //   print("Item \(index): \(element)")
            switch element{
                        case "north":
                            print("you've moved north")
                        case "east":
                            print("you've moved east")
                        case "south":
                            print("you've moved south")
                        case "west":
                            print("you've moved west")
                        default:
                            print("where you going?")
                        }
                        if index == 3{
                            print("round the world")
                        }
        }
    }
}

在您的代码中尝试访问第 4 个索引,因为您使用了 ... in 循环控制语法。第 4 个索引不在数组中。

这里是关于 swift 循环的一些细节。

for index in 0...4 {
 ...
}

上面的代码片段说,遍历从 0 开始并包含 4 的范围,即从 0–4

如果您不想包含 4,则使用这个称为半开范围运算符 (..<)。

for index in 0..<4 {
 ...
}

这将从 0 循环到 3 并停止执行。

所以,在这里你首先计算这里的数字 4。