Re: NSString intValue
Re: NSString intValue
- Subject: Re: NSString intValue
- From: Boyd Collier <email@hidden>
- Date: Thu, 4 May 2006 12:16:47 -0700
Thanks to everyone who offered suggestions. As I remarked in an
earlier posting, I had already tried NSScanner's scanInt: and the
problem with it was that if a user enters, for example, 5X, it
returns 5 and doesn't signal a problem. However, using isAtEnd: can
indeed be used to deal with this problem, so my code now reads
int myInt;
NSScanner *theScanner = [NSScanner scannerWithString:theString];
if ([theScanner scanInt:&myInt] && [theScanner isAtEnd] )
NSLog(@"The int represented by the string = %i", myInt); // for testing
else
NSLog(@"The string didn't represent an int"); // for testing
Incidentally, trying to simplify the above by using
NSScanner *theScanner = [NSScanner scannerWithString:[theTextField
stringValue]];
doesn't give the desired results. (I mention this for other newbies
who might be tempted to try it.)
Using NSNumberFormatter, which was suggested by several persons, may
be a preferable way to do what I want to do, and I'll certainly study
the documentation of this. However, for the time being, it looks more
complicated than the above, especially considering what the docs say
about changes made in 10.4.
Thanks again, Boyd
_______________________________________________
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