无法转换类型' [AnyObject]'的值预期参数类型' [String]'

时间:2016-07-08 19:29:06

标签: swift

当我尝试编译此代码时:

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

我收到此错误:

  

无法转换类型' [AnyObject]'的值预期参数类型' [String]'在这一行:

  def extension_white_list
    %w(jpg jpeg gif png) if model.avatar.file.extension.present?
  end

我做错了什么?

3 个答案:

答案 0 :(得分:0)

尝试以下方法:

  1. var idsArray = NSMutableArray()替换为var idsArray = [String]()。这样就可以使用Swift数组,而不是Obj-C NSArray s。

  2. 然后替换:

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

    使用:

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

答案 1 :(得分:0)

与此问题非常相似: Problems with code when updating to Swift 2.0. “Cannot convert value..”

如您所知,您需要将[String]传递给loadPlayersForIdentifiers(_:withCompletionHandler:)的第一个参数。而且playerId!肯定是String。为什么你NSMutableArray知道这个?

idsArray的声明更改为:

    var idsArray: [String] = []

导致错误的行:

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

答案 2 :(得分:0)

尝试将idArray声明为Array<String>()而不是NSMutableArray()

像这样:

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