user5862394
user5862394

Reputation:

Swift setText UILabel

let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print(responseString)
        self.lbl1.text = String(responseString)

Print Out:

Optional(2000 TL)

enter image description here

My problem

> "self.lbl1.text = String(responseString)" Optional(  write. 

I want to write "2000 TL"

Upvotes: 1

Views: 1293

Answers (2)

Tom el Safadi
Tom el Safadi

Reputation: 6796

Just try this:

print(responseString!)

Try this for your label:

self.lbl1.text = responseString!

This should remove the optional...

Upvotes: 1

dfrib
dfrib

Reputation: 73206

The immutable responseString is an optional, so you need to unwrap it to get rid of the Optional(...) wrapping.

You can do this e.g. using the nil coalescing operator

print(responseString ?? "") // Prints "" if responseString == nil
self.lbl1.text = String(responseString ?? "")

or optional binding

if let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) {
    print(responseString)
    self.lbl1.text = String(responseString)
}

Upvotes: 4

Related Questions