我有以下课程
class Game {
// An array of player objects
private var playerList: [Player]?
}
我想通过playerList枚举;这需要import Foundation
将其转换为NSArray
;但它总是抱怨它无法转换
func hasAchievedGoal() {
if let list:NSArray = playerList {
}
for (index,element) in list.enumerate() {
print("Item \(index): \(element)")
}
}
错误:
Cannot convert value of type '[Player]?' to specified type 'NSArray?'
我试过了:
if let list:NSArray = playerList as NSArray
我究竟做错了什么?
谢谢
您无需强制NSArray
进行枚举:
if let list = playerList {
for (index,value) in list.enumerate() {
// your code here
}
}
至于你的演员,你应该这样做:
if let playerList = playerList,
list = playerList as? NSArray {
// use the NSArray list here
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句