jefferyleo
jefferyleo

Reputation: 650

Get the current user custom column data in Swift Parse

As you can see the picture, this is the User class that allow user to login in my ios application

As I login via this user to my own application, after I've login, how do I get the custom column data of this user? For example, I want to get the IntakeCode from this user and show it in text field, how to I do that?

I've try this code, but it doesn't work, it says that

'AnyObject' is not convertible to 'String'; did you mean to use 'as!' to force downcast?

var displayIntake:NSString = PFUser.currentUser()!.objectForKey("IntakeCode")
txtIntake.text = displayIntake as String

Hope that someone can help with it, I just want that IntakeCode from this user show in the textfield.

Upvotes: 2

Views: 4508

Answers (3)

Rishabh Anand
Rishabh Anand

Reputation: 177

In swift 4 you just need to

import UIKit
import Parse

class newVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        var user = PFUser.current()
        print(user?.value(forKey: "isWhat"))
    }
}

Here is my parse server:-

enter image description here

This is my output in viewDidLoad of my View Controller enter image description here

Upvotes: 0

TomTom
TomTom

Reputation: 909

What you want to do is use a query, look into the parse doc for a reference to how to use a query but it should look something like this.

var query = PFUser.query
var currentUser = PFUser.currentUser()
query.whereKey("username", equalTo: currentUser.username)
query.getFirstObjectInBackgroundWithBlock {
  (object: PFObject?, error: NSError?) -> Void in
  if error != nil || object == nil {
    println("The getFirstObject request failed.")
  } else {
    // The find succeeded.
            self.intakeCode = object["IntakeCode"] as! String!

    println("Successfully retrieved the object.")
  }
}

This query well check the current users username with the database then return anything you call in the background method.

Upvotes: 1

siegy22
siegy22

Reputation: 4413

You can get any custom column using yourpfobject["yourcolumn"]..

In your case:

if let displayIntake = PFUser.currentUser()!["IntakeCode"] as? String {
    txtIntake.text = displayIntake
}

Upvotes: 6

Related Questions