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()
}
}
}
}
}
}
答案 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()
}
}
}
}
}
}