Search code examples
iosstringparse-platformpfuser

How do I get Parse data as a String out of PFUser?


I am currently trying to get a value called "loot" out of the current user. I need the value as a String, but Swift is being stubborn and says it "cannot convert Anyobject to String". The Parse documentation for iOS says to use something like:

let score = gameScore["score"] as String

and so, I try this :

let lootAmount = user["loot"] as String

BTW 'user' is referring to the current user. When I try that, it gives error saying it's not convertible. I tried placing '!'s and '?'s wherever Xcode suggested, but it just crashed the app with no error.

So, how do I get the user value called "loot" as a String?


Solution

  • Loot is an NSNumber not an NSString or String.

    You could convert it to a String like this:

    if let loot = user["loot"] as? NSNumber {
        let lootString = "\(loot)"
    }
    

    If you're not sure of an object's type, you can ask it using dynamicType:

    print(user["loot"]!.dynamicType)
    
    //prints `__NSCFNumber.Type`