Parse和Swift问题查询

时间:2015-07-10 03:11:21

标签: swift parse-platform

这是我的错误消息:

  

无法调用' getObjectInBackgroundWithId'使用类型'的参数列表(String,(PFObject!,NSError!) - > Void)'

这就是我的代码:

    func CallData(){

    var query : PFQuery = PFQuery(className: "QuestionsandAnswers")
    query.getObjectInBackgroundWithId("iXCrgCLKk2"){
        (Object : PFObject!, error : NSError!) -> Void in
        if (error == nil){
            self.Question = Object["Question"] as String!
            self.Answers = Object["Answers"] as Array!
            self.Answer = Object["Answer"] as String!

            if (self.Answers.count > 0){
                self.QuestionLabel.text = self.Question
            }
        } else {
            NSLog("There is something wrong!")
        }
    }
}

我尝试更改感叹号/问号/ PFObjectNSError后面没有任何内容,但没有任何效果。但是,如果我在PFObjectNSError后面加上一个问号,我会收到一条新的错误消息:

  

' AnyObject'不能转换为' String!&#39 ;;你的意思是使用'作为!'迫使低垂?

如果我将代码更改为as!,那么我会收到此错误:

  

无法指定类型'字符串的不可变值!'

有关如何解决此问题的任何想法?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

看看: http://blog.parse.com/announcements/building-apps-with-parse-and-swift/

我认为这会有用

var query = PFQuery(className: "QuestionsandAnswers")
    query.getObjectInBackgroundWithId("iXCrgCLKk2", block: { (object:PFObject?, error:NSError?) -> Void in
        if let error = error {
            //error
        } else if let object = object{
            //do something with your object as PFObject
            // such as
            var Question:String = object.objectForKey("Question") as! String

        }
    })