borna borna - 1 year ago 63
Swift Question

Option value are not getting assigned to an variable correctly

I have the following code that worked prior to Swift 3 and after I upgraded to Swift 3, It doesn't work right

let calendar = Calendar.current
let components = (calendar as NSCalendar).components([.hour, .minute], from: currentDate)
let hour = components.hour
let minutes = components.minute
var time = String(describing: hour) + ":" + String(describing: minutes)
var newTime = time.components(separatedBy: ":")
var ihh = Int(newTime[0])
var imm = Int(newTime[1])

var shh = String(ihh!)
var smm = String(imm!)

newTime array has 2 option values in it and I did validate that with debugger.

However after the values are assigned to ihh, imm, I see that both of them (ihh, imm) are nil.

Any idea how to fix this?


Answer Source

var time = String(hour! ) + ":" + String(minutes! )

'Describing ' adds "optional" word into your string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download