【问题标题】:Swift/Parse - Cannot invoke 'getObjectInBackgroundWithId' with an argument list of type '(PFUser?, (PFObject?, NSError?) -> Void)'Swift/Parse - 无法使用类型为“(PFUser?,(PFObject?,NSError?)-> Void)”的参数列表调用“getObjectInBackgroundWithId”
【发布时间】:2015-08-10 10:42:59
【问题描述】:

我在尝试搜索对象 UserDetail 时收到此错误消息。

错误信息

/Users/MNurdin/Documents/iOS/xxxxx/ViewController.swift:125:15: Cannot invoke 'getObjectInBackgroundWithId' with an argument list of type '(PFUser?, (PFObject?, NSError?) -> Void)'

我的代码

var user = PFUser.currentUser()

        var query = PFQuery(className:"UserDetail")
        query.getObjectInBackgroundWithId(user) { //error message here
            (gameScore: PFObject?, error: NSError?) -> Void in
            if error != nil {
                println(error)
            } else if let gameScore = gameScore {
                gameScore["cheatMode"] = true
                gameScore["score"] = 1338
                gameScore.saveInBackground()
            }
        }

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    您在传递给PFQuerygetObjectInBackground(_:block:) 方法的块中使用了错误的参数类型。根据the docsblock 参数采用NSArray(这可以桥接到AnyObject 的Swift 数组),并出现错误:

    query.getObjectInBackgroundWithId(user?.objectId) { (object: [AnyObject]?, error: NSError?) -> Void in
        // ...
    }
    

    此外,objectId 参数应该是一个字符串,但您似乎传入了一个 PFUser 实例。

    【讨论】:

      【解决方案2】:

      我不是专家,但查看文档我发现它接受字符串作为参数而不是对象。

      尝试使用:

      query.getObjectInBackgroundWithId(user?.objectId)

      【讨论】:

        【解决方案3】:

        这样使用getObjectInBackgroundWithId

        query.getObjectInBackgroundWithId("yourUser", block: {
                (gameScore: PFObject?, error: NSError?) -> Void in
        
                //your code
        
            })
        

        如您所见,getObjectInBackgroundWithId 的语法是:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-16
          • 1970-01-01
          • 2015-11-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多