1

When I try to compile this code:

func lookupPlayers() {
    print("Looking up \(match?.players.count)")
    
    // Loading ID from players connected in the match
    var idsArray = NSMutableArray()
    if (match != nil) {
        for players in match!.players {
            if let player = players as? GKPlayer {
                idsArray.addObject(player.playerID!)
            }
        }
        
    }
   
    GKPlayer.loadPlayersForIdentifiers(idsArray as [AnyObject] as [AnyObject], withCompletionHandler: { (players, error) -> Void in
        if (error != nil) {
            // Handle error here
            // if we fail to retrieve player info return and end the match
            print("Error retrieving player info: \(error.localizedDescription)")
            self.matchStarted = false
            self.delegate?.matchEnded?()
        }
        else {
            // Get info from all players and start the match
            self.playersDict = NSMutableDictionary(capacity: players.count)
            for player1 in players {
                if let player = player1 as? GKPlayer {
                    print("Found player: \(player.alias)")
                    self.playersDict.setObject(player, forKey: player.playerID)
                }
            }
            self.playersDict.setObject(self.localPlayer, forKey: self.localPlayer.playerID)
            
            self.matchStarted = true
            self.delegate?.matchStarted?()
        }
    })
}

I'm getting this error:

Cannot convert value of type '[AnyObject]' to expected argument type '[String]' in this line:

GKPlayer.loadPlayersForIdentifiers(idsArray as [AnyObject] as [AnyObject], withCompletionHandler: { (players, error) -> Void in

What am I doing wrong?

1
  • First of all error tells you that [AnyObject] should be a [String]. Second of all, why is there as [AnyObject] as [AnyObject]. I think the second as [AnyObject] is useless. It would be like saying treat this object as a car, which is treated as a car. Doesn't make sense. Have you tried simply replacing [AnyObject] with [String] and removing the duplicate in that line? Commented Jul 8, 2016 at 20:19

3 Answers 3

1

Very similar to this issue: Problems with code when updating to Swift 2.0. “Cannot convert value..”

As you know, you need to pass [String] fo the first parameter of loadPlayersForIdentifiers(_:withCompletionHandler:). And also playerId! definitely is a String. Why do you you NSMutableArray knowing that?

Change the declaration of idsArray as:

    var idsArray: [String] = []

And the line causing error to:

    GKPlayer.loadPlayersForIdentifiers(idsArray, withCompletionHandler: { (players, error) -> Void in
Sign up to request clarification or add additional context in comments.

Comments

0

Try the following:

  1. Replace var idsArray = NSMutableArray() with var idsArray = [String](). This way you use Swift arrays, not Obj-C NSArrays.

  2. Then replace:

GKPlayer.loadPlayersForIdentifiers(idsArray as [AnyObject] as [AnyObject], withCompletionHandler: { (players, error) -> Void in if (error != nil) {

With:

GKPlayer.loadPlayersForIdentifiers(idsArray as [String], withCompletionHandler: { (players, error) -> Void in if (error != nil) {

Comments

0

Try to declare idArray as Array<String>() instead of NSMutableArray().

Like this:

// Loading ID from players connected in the match
    var idsArray = Array<String>()
    if (match != nil) {
        for players in match!.players {
            if let player = players as? GKPlayer {
                idsArray.append(player.playerID!)
            }
        }

    }

    GKPlayer.loadPlayersForIdentifiers(idsArray, withCompletionHandler: { (players, error) -> Void in
        if (error != nil) {
            // Handle error here
         ...
}
    })

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.