1

ボタンがタップされると、UIAlertView がポップアップし、ユーザーが追加したいユーザーを入力するための textField を提供したいと考えています。このプロセスでは、Parse.com で正確なユーザー名を検索し、そのユーザーを currentUser の PFRelation に追加します。

これがSwiftのコードです。

@IBAction func addFriend(sender : AnyObject) {

    var currentUser = PFUser.currentUser()

    var alert = UIAlertController(title: "Add Friend", message: "Pleae enter a username.", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
        textField.placeholder = "Username"
        textField.secureTextEntry = false

        // Add button actions here
        var addBtnAction = UIAlertAction(title: "Add", style: UIAlertActionStyle.Default) {
            UIAlertAction in

            var text : String = textField.text

            println("I want to add the user: \(text)")

            var query = PFUser.query()
            query.whereKey("username", equalTo: text)
            query.getFirstObjectInBackgroundWithBlock {
                (object: PFObject!, error: NSError!) -> Void in
                if !object {
                    NSLog("The getFirstObject request failed.")
                    //add alertView here later

                } else {
                    // The find succeeded.
                    NSLog("Successfully retrieved the object.")
                    //This can find username successfully

                    var friendsRelation : PFRelation = currentUser.relationForKey("friendsRelation")
                    var user : PFUser = text  // <<<<---- This is the line that has problem
// Xcode tells me that I cannot express a String as a PFUser
// How can I add the user that has the exact same username?  

                }
            }
        }

        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
        alert.addAction(addBtnAction)
        self.presentViewController(alert, animated: true, completion: nil)

        })
}

これを読んでくれてありがとう。私を助けてください!!

4

1 に答える 1

1

あなたがやろうとしていることを正しく理解しているなら、次のようなことをするべきです:

friendsRelation.addObject(object);

以前に照会したobjectはどこですか。PFUser

于 2014-06-26T21:29:31.237 に答える