I just started learning Swift and I'm still confused about Optionals. I was wondering, what's the best way of doing it or what way should I follow?
let arrayOptional : [Int?] = [25, 30, nil, 50, nil]
// Optional Binding
var sum = 0
for i in arrayOptional {
if let temp = i {
sum += temp
}
}
sum = 0
// Forced Unwrapping
for i in arrayOptional {
if i != nil {
sum += i!
}
}
sum = 0
// ?? operator
for i in arrayOptional {
sum += i ?? 0
}
So are there any benefits of using one method over another?