iosparse-platformxcode6.3.1

Assigning PFUser with A PFObject Swift


How can I add a PFUser to a PFObject, here is my code now:

var gameScore = PFObject(className: "SiteLog-\(jobName.text)")
gameScore["TypeOfWorks"] = "\(typeOfWorks.text)"
gameScore["DateAndTime"] = "\(formattedDate)"
gameScore.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            // The object has been saved.
            println("sucessfully sent to parse!")
        } else {
            // There was a problem, check error.description
            println("Error Sending to Parse = \(error)")
        }

So how would I be able to assign a user to it, the user is already logged in so how can I assign this PFObject to the current user logged in!

I am using iOS Swift - Xcode 6.3.1

Thanks, George Barlow


Solution

  • In Objective-C you can reference the logged in user by calling [PFUser currentUser], which should translate to Swift as PFUser.currentUser(). Say you have a column for gameScore called user, you can add this line of code before saving gameScore:

        if let user = PFUser.currentUser(), username = user.username {
            user["column-name-in-parse-data"] = username
        }