I have a closure that adds zero to left part of a number in a condition and return String. The number type is Int but it must work if the Type is optional Int.
I ended up to repeat my code, my goal is stop repeating my code in the way that my code works for Int
and Int?
extension Int {
var addLeftZero: String { addLeftZeroClosure(self) }
}
extension Optional where Wrapped == Int {
var addLeftZero: String {
if let unwrappedValue: Wrapped = self { return addLeftZeroClosure(unwrappedValue) }
else { return "00" }
}
}
let addLeftZeroClosure: (Int) -> String = { value in
if (value >= 10) { return String(describing: value) }
else { return "0" + String(describing: value) }
}