Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 14, 2012
  1. @ganwell

    Allow creating custom NSFormatters. In combination with data binding …

    ganwell authored
    …this allows to get the standard "Please provide a valid value." dialogs.
  2. @ganwell

    Made methods in NSFormatterDelegate that through an exception when no…

    ganwell authored
    …t implemented abstract, so the developer has to implement them (StringFor, ObjectFor).
This page is out of date. Refresh to see the latest.
Showing with 63 additions and 0 deletions.
  1. +63 −0 src/Foundation/NSFormatter.cs
View
63 src/Foundation/NSFormatter.cs
@@ -0,0 +1,63 @@
+//
+// Authors:
+// Jean-Louis Fuchs
+//
+// Copyright 2012
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace MonoMac.Foundation {
+
+ public abstract class NSFormatterDelegate : NSFormatter {
+
+ public NSFormatterDelegate (IntPtr handle) : base (handle)
+ {
+ }
+
+ [Export ("initWithCoder:")]
+ public NSFormatterDelegate (NSCoder coder) : base (coder)
+ {
+ }
+
+ [Export ("getObjectValue:forString:errorDescription:")]
+ private bool _ObjectFor (IntPtr objectFor, string forString, IntPtr errorDescription)
+ {
+ string errorDescriptionString = null;
+ NSObject outObjectFor = null;
+ bool ret = ObjectFor (ref outObjectFor, forString, errorDescription != IntPtr.Zero, ref errorDescriptionString);
+ if (outObjectFor != null) {
+ Marshal.WriteIntPtr (objectFor, outObjectFor.Handle);
+ }
+ if (errorDescription != IntPtr.Zero && string.IsNullOrEmpty (errorDescriptionString) == false) {
+ NSString errorDescriptionObj = new NSString (errorDescriptionString);
+ Marshal.WriteIntPtr (errorDescription, errorDescriptionObj.Handle);
+ }
+ return ret;
+ }
+
+ public override abstract string StringFor (NSObject value);
+ public abstract bool ObjectFor (ref NSObject objectFor, string forString, bool needsErrorDescription, ref string errorDescription);
+
+ }
+}
Something went wrong with that request. Please try again.