0
    let username = self.user?.getProperty("username") as? String
    self.navigationItem.title = "@\(username)"

What I want to happen there is for it to print on the screen that users username with an @ in front of it like @user2

What it is printing instead is @Optional("user2")

How do I make this stop that? Ha

Jordan
  • 391
  • 4
  • 20

1 Answers1

0

String Interpolation prints also literal Optional(...) if the value is an optional.

To avoid that use either optional binding

if let username = self.user?.getProperty("username") as? String {
   self.navigationItem.title = "@\(username)"
}

Or the ternary conditional operator

let username = self.user?.getProperty("username") as? String
self.navigationItem.title = username != nil  ? "@\(username!)" : ""

In the first example the title won't be updated if username is nil, in the second it's updated with an empty string.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks. Much appreciated. Still learning my way through this. Ill check yours once the timer allows it. – Jordan Jun 20 '16 at 16:52