Here is my code in Swift
@IBAction func addUser(sender: AnyObject) {
var query = PFUser.query()
query.whereKey("username", equalTo: usernameTextField)
query.getFirstObjectInBackgroundWithBlock{ (objects:PFObject!, error: NSError!) -> Void in
if error == nil {
var friendList = PFUser.currentUser().relationForKey("friends") // I don't understand relationforKey, what does it do? and what happen inside ( )?
var addFriend = objects
if addFriend != nil {
PFRelation.addObject(addFriend!) // <- error here
println("added")
}
PFUser.currentUser().saveInBackgroundWithBlock{
(succeeded: Bool!, error: NSError!) in
if error != nil {
println("Error")
}
else {
println("saved")
}
}
}
}
}
I want to let my app be able to add other users via their username with Parse and I have trouble with PFRelation.addObject(addFriend!). It shows that I cannot add PFObject into this method since inside ( ) has to be PFRelation object but I don't know how to convert this.
Any helps is appreciated. Thank you!
First of all, make sure that you have the correct naming convention. Does
usernameTextFieldis an instance ofUITextFieldorString? Be careful for it.For your question, your
PFRelationdoes not know which objects to associate. How can it relate usingaddObjectthen? Also, you can not directly calladdObjectusingPFRelationbecauseaddObjectis not a factory method.Here is something that you need to do, I just type it here and it might have errors: