Allow creating custom NSFormatters #25

Closed
wants to merge 2 commits into
from

2 participants

@ganwell

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.
2f47085
@ganwell ganwell Made methods in NSFormatterDelegate that through an exception when no…
…t implemented abstract, so the developer has to implement them (StringFor, ObjectFor).
cf1365f
@migueldeicaza
Mono Project member

Hello,

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?

@ganwell

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:

https://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/DataFormatting/Articles/CreatingACustomFormatter.html

Then there is this guide:

http://macdevcenter.com/pub/a/mac/2006/08/08/nstableview-class.html?page=4

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

http://ganwell.0xcb.ch//2012-03-01T21-07-04..png

Best,
Jean

@migueldeicaza
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.

@ganwell

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