Allow creating custom NSFormatters #25

wants to merge 2 commits into

2 participants


This pull request has two parts (monomac and maccore).

In combination with data binding this allows to get the standard "Please provide a valid value." dialogs.

Please note:

  • I added a sample in monomac (CustomNSFormatter)
  • I had to implement getObjectValue:forString:errorDescription: in a subclass of NSFormatter, otherwise it will raise an exception, complaining that is not implemented in a subclass. (Probably this could also be solved with some ObjCRuntime magic.)
  • I called that class NSFormatterDelegate, which is not completely correct. It is used as a delegate class but not for NSFormatter. You know a better name?
  • I could mark the two methods that must be implemented when subclassing NSFormatter abstract. Which is nice because developers can't get the exceptions anymore.
  • The design is not completely standard, but the problem wasn't standard either.
ganwell added some commits Feb 14, 2012
@ganwell ganwell Allow creating custom NSFormatters. In combination with data binding …
…this allows to get the standard "Please provide a valid value." dialogs.
@ganwell ganwell Made methods in NSFormatterDelegate that through an exception when no…
…t implemented abstract, so the developer has to implement them (StringFor, ObjectFor).
Mono Project member


I am hesitant to add this code as is, because of the subclass of NSFormatter that is introduced. Both because of its naming and because I do not really understand if this is the right approach.

Do you have a pointer to an Objective-C sample that shows how people are actually using this?


Hi Miguel,

I am hesitant, too, probably the MonoMac users should subclass NSFormatter, but it requires a lot of research, at least the way I did it. (IntPtrs) I wanted to spare them the work and the errors. It would be great if you could build a better solution.

First there is the Creating a Custom Formatter document from apple:

Then there is this guide:

You can subclass NSFormatter for conversion and for validation. Here is what OS X will display if the validation fails:


Mono Project member

The name for the subclass is likely wrong, the pattern for Foo and FooDelegate is that Foo has a property called Delegate that points to a FooDelegate. In this case FooDelegate is a subclass of Foo.


What about NSCustomFormatter? This should indicate the purpose without violating the naming convention.

@ganwell ganwell closed this Feb 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment