• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Making NSNumberFormatter empty-tolerant
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Making NSNumberFormatter empty-tolerant


  • Subject: Re: Making NSNumberFormatter empty-tolerant
  • From: bryscomat <email@hidden>
  • Date: Thu, 27 Aug 2009 14:46:06 -0500

Interesting. Is that documented anywhere? That's why I was confused as to exactly how I got mine to work. Seems like a fluke...

On Aug 27, 2009, at 2:39 PM, Frédéric Testuz wrote:

Le 27 août 09 à 20:45, Markus Spoettl a écrit :

Hello,

before I get to the question this is my setup:

1) NSTextField ("field")
2) bound to an NSNumber property ("number") of an object.
3) "field" has an NSNumberFormatter attached with all default
values (as set up by IB), except maximum fraction digits is set to 1.
4) "number" can be nil - in fact it is when we start our app.


Starting this little app, one can tab into and out-of an the empty field, nothing happens. This as expected and what I want.

When you enter a value and tab out, "number" gets set to the new value. If you now attempt to remove the value by emptying the field you will get formatting error message. This is kind of unexpected as the empty field was fine with the formatter moments ago.

In other words, once the value is set to something non-nil it never can get nil again. I would like to be able to nil out a number by clear a field.

My solution for this is a sub-class of NSNumberFormatter which implements -getObjectValue:forString:range:error: as follows:

- (BOOL)getObjectValue:(out id *)anObject
forString:(NSString *)aString
range:(inout NSRange *)rangep
error:(out NSError **)error
{
BOOL result = [super getObjectValue:anObject forString:aString range:rangep error:error];


if (!result && ((aString == nil) || ([aString isEqualToString:@""]))) {
*anObject = nil;
result = YES;
}


  return result;
}

The docs tell me that I have to feed a valid NSNumber into anObject when returning YES - nil is valid therefore this should be OK.

The question is: Is this safe? Do I overlook an aspect that I'd better not? Or even better, is there a simpler solution?

I had the same problem. For the 10.4 behaviour of NSNumberFormatter, you don't have to subclass it.


Just set the nil symbol with:

[myNumberFormatter setNilSymbol:@""];

Alternatively, you can set it in IB:

In the number formatter attribute palette, go down until you see Nil Symbol, enter a character and then delete it.


Note: if only the NSDateFormatter has the same attribute.

Frédéric_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Making NSNumberFormatter empty-tolerant
      • From: Frédéric Testuz <email@hidden>
References: 
 >Making NSNumberFormatter empty-tolerant (From: Markus Spoettl <email@hidden>)
 >Re: Making NSNumberFormatter empty-tolerant (From: Frédéric Testuz <email@hidden>)

  • Prev by Date: Re: Making NSNumberFormatter empty-tolerant
  • Next by Date: Launching a document-based app with no document
  • Previous by thread: Re: Making NSNumberFormatter empty-tolerant
  • Next by thread: Re: Making NSNumberFormatter empty-tolerant
  • Index(es):
    • Date
    • Thread