Jake Walker Jake Walker - 7 months ago 24
iOS Question

How do I not get optional in the output?

I have a welcome string that says Hello and if the user has entered a

when registering and thus it is not equal to nil then it says Hello Jake or whatever. However, on the screen it says
"Hello Optional("Jake")"
. Now I am very new to swift but I do know about the concept force unwrapping. I tried putting an exclamation mark at different points and tried

let personsName = user["FirstName"] as! String

to say it is definitely a string but that didn't work either.

welcomeLabel.text = "Hello"
let user = PFUser.current()!
if let personsName = user["FirstName"] {
welcomeLabel.text = "Hello " + String(personsName)


You can cast to String which will unwrap the value

// You user might be like this 
let user:[String: AnyObject?] = ["FirstName": "Joe"]

if let personsName = user["FirstName"] as? String {
  print (  "Hello  \(personsName)")  // Hello  Joe

if let personsName2 = user["FirstName"] {
    print ("Hello  \(personsName2)") // Hello  Optional(Joe)