Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More specific NSNumberFormatter failure behaviour

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?

like image 882
Simon Broadhead Avatar asked Apr 26 '10 21:04

Simon Broadhead


1 Answers

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.

like image 100
JeremyP Avatar answered Sep 28 '22 19:09

JeremyP