Swift致命错误:索引超出范围(超出范围索引)

时间:2020-05-28 03:50:42

标签: ios swift iphone

你好,我想学习快速。我对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()”上收到此错误

错误:执行被中断,原因:EXC_BAD_INSTRUCTION (代码= EXC_I386_INVOP,子代码= 0x0)。

这是控制台输出:

您已经向北移动

您已经东移

您已经向南移动

您已向西移动

环游世界

致命错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, 444行

4 个答案:

答案 0 :(得分:3)

快速地,有更有效的循环方式...但是可以更好地了解您实现的方式...

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

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中的快乐编码:-)

答案 1 :(得分:1)

由于您已在循环控制语法中使用...,因此在您的代码中尝试访问第4个索引。而且第四个索引不在数组中。

这是有关快速循环的一些细节。

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

上面的代码段说,在0到4(即从0–4)开始的范围内进行迭代

如果您不希望包含4,则使用此方法称为半开范围运算符(.. <)。

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

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

答案 2 :(得分:0)

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")
                        }
        }
    }
}

答案 3 :(得分:0)

所以,在这里,您首先要对此处为4的num进行计数。

enter image description here