• 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: NSNumberFormattter not working
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSNumberFormattter not working


  • Subject: Re: NSNumberFormattter not working
  • From: Ken Tozier <email@hidden>
  • Date: Thu, 12 Jul 2007 14:51:28 -0400


On Jul 12, 2007, at 2:34 PM, Alastair Houghton wrote:

On 12 Jul 2007, at 18:58, Ken Tozier wrote:

Here's what I think should happen, as pseudo code, in response to key presses (if NSTextField allowed access to individual keypresses)

- (void) keyDown:(NSEvent *) inEvent
{
unichar aChar = [[inEvent charactersIgnoringModifiers] charAtIndex: 0];

if ((aChar >= '0') && (aChar <= '9'))
/* accept the character */
else
/* reject the character */
}


Simple clean, effective.

...and would never work.

It doesn't take account of copy & paste, or drag & drop, or the possibility that the user is using an IME of some sort, or accessibility features, or speech recognition. I'm sure others can think of other things that won't work if you do that too.

Amusingly it's often Windows programmers who suggest things like the above, because it's what they'd usually do there, and it doesn't even work right on Windows(!)

For my current app, the number field is just going to be used as a way to allow users to change the page number of a news page so there is no need for cut and paste or drag and drop. That keyDown handler would be perfect for my limited needs.



- (BOOL) getObjectValue:(id *) outObj forString:(NSString *) inString errorDescription:(NSString **) outError { int intResult; NSScanner *scanner; BOOL exeOK = NO;

scanner = [NSScanner scannerWithString: string];

// Huh? What the heck is this for?
[scanner scanString: @"$" intoString: NULL]; //ignore return value

It looks like the code is trying to skip leading dollar signs. This isn't such a great idea, by the way... some of us don't use the U.S. Dollar, so unless you're working on an application that specifically deals with U.S. Dollars and won't be useful to the rest of the world (e.g. a program for helping complete an IRS tax return), you should probably make more of an effort if you're going to suppose currency amounts.

That was just a direct copy/paste from an example in the NSFormatter documentation.

That's not right. I'm assuming that you don't want currency support, you probably want something like this:


  - (BOOL)isPartialStringValue:(NSString *)partialString
              newEditingString:(NSString **)newString
              errorDescription:(NSString **)error
  {
    NSRange range;

    range = [partialString rangeOfCharacterFromSet:gNotADigitCharSet];

    if (range.position == NSNotFound)
      return YES;

*error = NSLocalizedString (@"You must enter a number", @"You must enter a number");
*newString = nil;
return NO;
}


Of course, you'll need to use NSCharacterSet's -invertedSet method to generate the gNotADigitCharSet.

(Even better, you could use the extremely verbosely named

- isPartialStringValid:proposedSelectedRange:originalString:originalSele ctedRange:errorDescription:

method. The one you've used above is a compatibility method and isn't as powerful.)

I'll keep that tip for future reference as I just need a really simple integer filter for now.


Thanks for the feedback Alastair. Now to get it working...

Ken
_______________________________________________

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: NSNumberFormattter not working
      • From: Alastair Houghton <email@hidden>
References: 
 >Re: NSNumberFormattter not working (From: Keary Suska <email@hidden>)
 >Re: NSNumberFormattter not working (From: Ken Tozier <email@hidden>)
 >Re: NSNumberFormattter not working (From: Alastair Houghton <email@hidden>)

  • Prev by Date: Re: Cross-platform toolkit with a Cocoa backend
  • Next by Date: Re: NSNumberFormattter not working
  • Previous by thread: Re: NSNumberFormattter not working
  • Next by thread: Re: NSNumberFormattter not working
  • Index(es):
    • Date
    • Thread