i6x86
i6x86

Reputation: 1627

Cannot convert value of type '[AnyObject]' to expected argument type '[String]'

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?

Upvotes: 1

Views: 4227

Answers (3)

Jenny Tran
Jenny Tran

Reputation: 553

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
         ...
}
    })

Upvotes: 0

Lawrence413
Lawrence413

Reputation: 2016

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) {

Upvotes: 0

OOPer
OOPer

Reputation: 47896

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

Upvotes: 1

Related Questions