Setting Parse Pointer to Class Other than PFUser

荒凉一梦 提交于 2019-12-13 06:36:31

问题


I am trying to set a pointer from an object in "Classes" to an object in "Schools". When I do so, I get this error:

invalid type for key school, expected *Schools, but got string (Code: 111, Version: 1.12.0)

I know that the error is saying that it expects a pointer for class, "Schools", but it got a string instead. Here is my code:

let schoolObjectIdQuery = PFQuery(className:"Schools")
        schoolObjectIdQuery.whereKey("school_name", equalTo: "\(PFUser.currentUser()!.objectForKey("school_name")!)")

        let query = PFQuery.orQueryWithSubqueries([schoolObjectIdQuery])

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

            if let objects = results {

                for object in objects {

                    let schoolObjectId = object.objectId!

                    //////////////////

                    let classInfo = PFObject(className: "Classes")

                    classInfo.setObject(className!, forKey: "class_name")
                    classInfo.setObject(teacherName!, forKey: "teacher_name")
                    classInfo["school"] = schoolObjectId

                    classInfo.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in

                        if error != nil {

                            spinningActivity.hideAnimated(true)

                            self.displayAlert("Error", message: error!.localizedDescription)

                            print(error?.localizedDescription)        
                        }
                        else if success {

                            spinningActivity.hideAnimated(true)

                            self.dismissViewControllerAnimated(true, completion: nil)        
                        }
                        else {

                            self.displayAlert("Something Went Wrong", message: "Please try again")               
                        }
                    })  
                }  
            }
        })

The above code is querying for the objectId of the correct "Schools" object and then setting data to another class, "Classes", for which I need the objectId from the "Schools" object. * I know my error handling isn't set up properly yet

If I replace classInfo["school"] = schoolObjectId with classInfo["school"] = PFUser.currentUser() the pointer is sent to Parse with no problem. How can I set the pointer to "school_name" in class "Schools" as a pointer rather than a string?

Thanks in advance for the help!


回答1:


a Pointer in Parse isn't just the objectId. You have to invert the ParseObject, not only the objectId.

PFUser.currentUser() returns the User Parse Object. Thats why it works.

try this:

classInfo["school"] = object


来源:https://stackoverflow.com/questions/36047697/setting-parse-pointer-to-class-other-than-pfuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!