注册后向 PFUser 添加列?解析,Swift

Add column to PFUser AFTER signup? Parse, Swift

我希望我的用户在注册我的应用程序后 add/edit 有关他们个人资料的详细信息。

@IBAction func doneEditting(sender: AnyObject) {
    self.completeEdit()
}

func completeEdit() {
    var user = PFUser()
    user["location"] = locationTextField.text

    user.saveInBackgroundWithBlock {
        (succeeded: Bool, error: NSError?) -> Void in
        if let error = error {
            let errorString = error.userInfo?["error"] as? NSString
            println("failed")
        } else {
            self.performSegueWithIdentifier("Editted", sender: nil)
        }
    }
}

断点正好停在user.saveInBackgroundWithBlock。没有文档显示如何在注册后附加新列。

谢谢!

Parse 允许您延迟向 class 添加列,这意味着您可以向 PFObject 添加一个字段,如果它不存在于您的 Parse class,Parse 将添加该列给你。

下面是如何通过代码添加列的示例:

// Add the new field to your object
yourObject["yourColumnName"] = yourValue
yourObject.saveInBackground()

您会注意到 Parse 将在其门户网站上创建一个名为 yourColumnName 的新列。

参考自

您提到用户在注册后应该能够编辑他们的个人资料。当使用 signUpInBackgroundWithBlock 向 Parse 注册用户时,Parse SDK 会自动为您创建一个 PFUser

在您提供的代码中,您正在创建并保存一个全新的 PFUser,而不是获取当前登录的代码。如果您没有使用已登录的 PFUser,则您将在 user.saveInBackgroundWithBlock 处收到以下错误(您也在 post 中提到):

User cannot be saved unless they are already signed up. Call signUp first

要解决此问题,您需要更改:

var user = PFUser()

以下内容:

var user = PFUser.currentUser()!

您的其余代码(例如 user["location"] = locationTextField.text)工作正常,并且会 dynamically/lazily 添加一个新列到您的 User 数据库(这就是您想要的)。