Reputation: 650
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
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:-
This is my output in viewDidLoad of my View Controller
Upvotes: 0
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
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