I have an NSTextField
into which I need the user to enter a number between a max and min, and it would be nice if I could detect when the NSNumberFormatter
fails that particular test so I can either display a nicer message ("The number is too large" is not very helpful, it needs to display the valid range) or simply set the field automatically to the nearest valid value.
I've looked at the NSTextField
delegate's ‑control:didFailToFormatString:errorDescription:
method which doesn't seem to allow you to modify the error, and I've looked at overriding the NSNumberFormatter
's ‑getObjectValue:forString:range:error:
method which does give me an NSError
that I can modify, but there doesn't seem to be any way to determine which specific error was returned.
Since I am just entering a simple integer, I don't need most of the functionality in NSNumberFormatter
, would I be better off just writing my own formatter from scratch?
There is a way to determine which error is returned in an NSError. The NSError error codes documentation defines errors for both max and min validation fails. Just send -code to your NSError object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With