Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing value of a optional variable includes the word "Optional" in Swift [duplicate]

Tags:

ios

swift

.I am new to iOS and to Swift In my application I'm trying to print an optional value and it prints "Optional(value of the variable)" How do I remove this word optional

var bDay = StringUtils.convertDateToString(birthDate, format: Constants.BIRTHDAY_FORMAT)
let age = self.clientDetail?.getAge()
println("age.....\(age)")
bDay += "\(age)"

The output in the console is

age.....Optional(29)

I'm trying to assign this variable to a UILabel but on screen it shows up like Sep 17, 1986 Optional(29)

My objective is to remove this optional word and make it appear like Sep 17, 1986(29)

Thanks in advance

like image 907
Abhilasha Avatar asked Oct 19 '25 19:10

Abhilasha


1 Answers

Optional chaining is used here:

let age = self.clientDetail?.getAge()

So return of getAge() is optional value. Try optional binding:

if let age = age {
    println("age.....\(age)")
}

or simply unwrap the age with age!, but this will crash your app if age is nil.

like image 144
Kirsteins Avatar answered Oct 22 '25 09:10

Kirsteins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!