在数组中使用for循环值时,EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

时间:2017-01-12 14:22:08

标签: swift3

在尝试使用变量声明我想要读取的元素时,我一直收到此错误。我到处寻找解决方案,但我无法找到适合我特定情况的解决方案,任何帮助都将不胜感激。提前谢谢。

getListOfFiles::FilePath -> IO [FilePath]
getListOfFiles fpath = FS.getDirectoryContents fpath

variable declaration

code with error

1 个答案:

答案 0 :(得分:0)

你混淆了for循环类型:

var segmentStore : [Int] = [0,1,2]

//Option A

for index in 0...segmentStore.count-1 {
    var element:Int = segmentStore[index];

    if element == 0 {
        print("Zero");
    } else if element == 1 {
        print("One");
    } else if element == 2 {
        print("Two");
    }else {
        print("Other");
    }
}

//Option B
for element: Int in segmentStore {
    if element == 0 {
        print("Zero");
    } else if element == 1 {
        print("One");
    } else if element == 2 {
        print("Two");
    }else {
        print("Other");
    }
}