问题
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