对返回nil的Pointer值进行解析查询

时间:2016-03-27 04:25:18

标签: ios swift parse-platform swift2

我有一个表UserProfile,它有一个指向相应User表的Pointer列(用户)。我正在尝试构建一个简单的查询来抓取用户对象的UserProfile。但是,查询返回nil(object =我的应用程序中的用户对象):

let profileQuery = PFQuery(className: "UserProfile")
                    profileQuery.whereKey("user", equalTo: object)
                    profileQuery.includeKey("user")
                    profileQuery.limit = 1
                    profileQuery.findObjectsInBackgroundWithBlock{
                        (results: [PFObject]?, error: NSError?) -> Void in

                        if error != nil{
                            print(error)
                        } else if let results = results as? [PFObject]! {

                            for result in results {
                                self.profile = result
                            }
                        }
                    }

知道为什么这个查询不起作用?谢谢!

使用Mazel的解决方案更新了代码:

query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        if error != nil {

            print(error)

        } else if let objects = objects as? [PFObject]! {

            for object in objects {

      let profileQuery = PFQuery(className: "UserProfile")
                    profileQuery.whereKey("user", equalTo: PFObject(outDataWithClassName: "_User", objectId: "\(object.objectId!)"))
                    //profileQuery.includeKey("user")
                    profileQuery.limit = 1
                    profileQuery.findObjectsInBackgroundWithBlock{
                        (results: [PFObject]?, error: NSError?) -> Void in

                        if error != nil{
                            print(error)
                        } else if let results = results as? [PFObject]! {

                            for result in results {
                                self.profile = result
                            }
                        }
                    }

1 个答案:

答案 0 :(得分:0)

也许指针的存储方式不同,尝试使用此

profileQuery.whereKey("user", equalTo: PFObject(outDataWithClassName: "_User", objectId: "\(object.objectId!)"))

你也可以删除这行profileQuery.includeKey(“user”)