1

我希望当点击按钮时,会弹出一个 UIAlertView 并为用户提供一个 textField 以键入他想要添加的用户。该过程涉及在 Parse.com 上搜索 EXACT 用户名并将用户添加到 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 回答