------------------------------
Message: 5
Date: Thu, 12 Jul 2007 14:51:28 -0400
From: Ken Tozier <email@hidden>
Subject: Re: NSNumberFormattter not working
To: "Cocoa-Dev (Apple)" <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
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:originalSel
e
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