• 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:02:43 -0500

I solved this before by putting a single space in the Null Placeholder spot on the bindings panel in IB.
It worked for me.


On Aug 27, 2009, at 1:45 PM, Markus Spoettl wrote:

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?

Regards
Markus
--
__________________________________________
Markus Spoettl

_______________________________________________

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


References: 
 >Making NSNumberFormatter empty-tolerant (From: Markus Spoettl <email@hidden>)

  • Prev by Date: Making NSNumberFormatter empty-tolerant
  • Next by Date: Re: Making failed NSAsserts crash an app
  • Previous by thread: Making NSNumberFormatter empty-tolerant
  • Next by thread: Re: Making NSNumberFormatter empty-tolerant
  • Index(es):
    • Date
    • Thread