Re: NSNumberFormattter not working
Re: NSNumberFormattter not working
- Subject: Re: NSNumberFormattter not working
- From: Alastair Houghton <email@hidden>
- Date: Thu, 12 Jul 2007 19:34:07 +0100
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(!)
And here's the integer franken-formatter so far. (please tell me
this isn't "the right" way to do this)
Well it is, roughly speaking, yes. I'd make one or two changes to
the code as you have it below.
[snip]
- (BOOL) getObjectValue:(id *) inObj
forString:(NSString *) inString
errorDescription:(NSString **) inError
{
int resInt = [inString intValue];
*obj = [NSNumber numberWithString:
}
This doesn't seem to be right. You *might* want *inObj =
[NSNumberWithString:inString], but given what you've written later it
looks like you're trying to scan a number of dollars, so you might
need to use NSScanner instead.
- (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.
[snip]
- (BOOL) isPartialStringValid:(NSString *) partialString
newEditingString:(NSString **) newString
errorDescription:(NSString **) error
{
NSLog(@"entered KIntegerFormatter:isPartialStringValid");
if ([partialString length] == 0)
return YES;
else
{
int lastCharIndex = [partialString length] - 1;
unichar lastChar = [partialString characterAtIndex:
lastCharIndex];
if ([gDigitCharSet characterIsMember: lastChar])
{
return YES;
}
else
{
*newString = nil;
return NO;
}
}
}
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:originalSelect
edRange:errorDescription:
method. The one you've used above is a compatibility method and
isn't as powerful.)
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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