I have the following class
class Game {
// An array of player objects
private var playerList: [Player]?
}
I want to enumerate through the playerList; this requires to import Foundation
then cast it to a NSArray
; but its always complaining that it can't convert it
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?'
I've tried the below, but that isn't working:
if let list:NSArray = playerList as NSArray
You don't need to cast to NSArray
to enumerate:
if let list = playerList {
for (index,value) in list.enumerate() {
// your code here
}
}
As for your cast you should do it like this:
if let playerList = playerList,
list = playerList as? NSArray {
// use the NSArray list here
}