更新Swift 2中的枚举函数

时间:2015-10-29 12:29:00

标签: ios swift

Xcode说“'enumerate'不可用:在序列上调用'enumerate()'方法” 当我尝试使用新语法

for (row, rowArray) in tilesArray.enumerate() {

然后弹出错误“类型'AnyObject'的值没有成员'枚举'” 我究竟做错了什么?

init (filename: String) {
    if let dictionary = Dictionary<String, AnyObject>.loadJSONFromBundle(filename) {
        if let tilesArray: AnyObject = dictionary["tiles"] {
            for (row, rowArray) in enumerate(tilesArray as! [[Int]]) {
                let tileRow = NumRows - row - 1
                for (column, value) in enumerate(rowArray) {
                    if value == 1 {
                        tiles[column, tileRow] = Tile()
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您显然知道tilesArray的类型为[[Int]],因此对正确类型的可选绑定也是如此

init (filename: String) {
  if let dictionary = Dictionary<String, AnyObject>.loadJSONFromBundle(filename) {
    if let tilesArray = dictionary["tiles"] as? [[Int]] {
        for (row, rowArray) in tilesArray.enumerate() {
            let tileRow = NumRows - row - 1
            for (column, value) in rowArray.enumerate() {
                if value == 1 {
                    tiles[column, tileRow] = Tile()
                }
            }
        }
    }
  }
}