Printing extra variable value includes the word "Optional" in Swift

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

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

      

Console output

age.....Optional(29)

      

I am trying to assign this variable to UILabel, but it appears on the screen as September 17, 1986. More (29)

My goal is to remove this optional word and make it look like September 17, 1986 (29)

Thank you in advance

+3


source to share


1 answer


Additional chaining is used here:

let age = self.clientDetail?.getAge()

      

Therefore, return getAge()

is optional. Try optional linking:



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

      

or just unpack age

with age!

, but this will crash your application if age

zero.

+8


source







All Articles