Can you use nil coalescing operator ?? to expand String? and use the default value "" , which, as you know, will result in nil :
return Int(value ?? "")
Another approach: an int initializer that takes a String?
From the comments:
It is very strange to me that the initializer did not agree with the optional and would simply return nil if any of them were passed.
You can create your own initializer for Int , which does just that:
extension Int { init?(_ value: String?) { guard let value = value else { return nil } self.init(value) } }
and now you can just do:
var value: String? return Int(value)
source share