• 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: NSString intValue
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString intValue


  • Subject: Re: NSString intValue
  • From: Bill Bumgarner <email@hidden>
  • Date: Wed, 3 May 2006 08:49:24 -0700

On May 2, 2006, at 11:25 PM, Paolo Bertani wrote:
Boyd,
if you need to check that the text entered by the user is an integer value you may try this:


convert the NSString to int, convert the int back to NSString, compare the resulting NSString with the original one. If they are equal then there are no mistakes on the text.

That won't work for cases where the user enters numbers in a non- normalized form. 10.0 vs. 10 or 00100 vs. 100, etc...


BTW: if the user enters the text on a text field, I would check the input every time a new char is entered, playing a beep if the user types a letter instead of a number.

No need to re-invent this wheel. Just attach an NSNumberFormatter to the field in IB (or programmatically) and use it to automatically validate the numeric input....


And if you want to do any custom parsing, the formatters are really easy to override.

For example, the following is the entire implementation of an NSDateFormatter subclass that will accept raw integers as "number of days from now" type input. You would do something similar to subclass NSNumberFormatter. Once subclassed, drag your custom class's .h file into IB to make IB aware of the class, then click on the field that needs validation and set the formatter class to your custom formatter class.

@implementation DateFormatterPlusDays
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)error;
{
NSScanner *scanner;
int intResult;


scanner = [NSScanner scannerWithString: string];
if ([scanner scanInt:&intResult] && ([scanner isAtEnd])) {
if (obj)
*obj = [[NSCalendarDate calendarDate] dateByAddingYears: 0 months:0 days:intResult hours:0 minutes:0 seconds:0];
return YES;
} else {
return [super getObjectValue: obj forString: string errorDescription: error];
}
}
@end



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >NSString intValue (From: Boyd Collier <email@hidden>)
 >Re: NSString intValue (From: glenn andreas <email@hidden>)
 >Re: NSString intValue (From: Boyd Collier <email@hidden>)
 >Re: NSString intValue (From: Paolo Bertani <email@hidden>)

  • Prev by Date: Re: Efficiently getting characters
  • Next by Date: Re: Incorrect flipping when manually drawing NSCells (was: Fwd: -isFlipped not working right when drawing IconAndTextCell to NSImage)
  • Previous by thread: Re: NSString intValue
  • Next by thread: Re: NSString intValue
  • Index(es):
    • Date
    • Thread