Hi everybody
In my custom NumberTextField
I'm currently writing, I'm trying to generically convert String
s to the corresponding BinaryInteger
(e.g. Int16
or Int
) using the newly introduced initializer: init(String, format: IntegerFormatStyle<Self>, lenient: Bool)
https://developer.apple.com/documentation/swift/binaryinteger/3869983-init
(Yes, currently there is no documentation available @Apple :-))
I hit the following roadblock: the function is defined as throwing, and I am trying to catch the exception.
But the function is causing a fatal error in the application:
Fatal error: Not enough bits to represent the passed value
You can try it yourself in a playground with the following code:
import Foundation
var tooBig = "32800"
if let value = try? Int16(tooBig, format: IntegerFormatStyle.number) {
print("valid number", value)
} else {
print("conversion issue")
}
What methods are you using to convert a (user provided) string to a number in Swift?
PS: Note, I'm using this under macOS, but I suppose the problem is the same on iOS