How does one find a '.' in a string object in Object-C
I am working on getting a simple calculator working as part of my adventure to learning Object-C and iOS development.
In Object-C using NSString, how does one look for a period in a string?
Based开发者_StackOverflow on the comments this is what I got so far.
NSString * tmp = [display text];
NSLog(@"%@", tmp); // Shows the number on the display correctly
int x = [tmp rangeOfString:@"."].location;
NSLog(@"%i", x); // Shows some random signed number
if (x < 0) {
[display setText:[[display text] stringByAppendingFormat:@"."]];
}
It is still not working :(
NSRange is a straight-up struct...
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
int x = [@"hello.there.ok" rangeOfString:@"."].location;
printf("x is %d\n",x);
prints 5.
and here is a whole program:
int main(int argc, char *argv[])
{
NSString *ht = @"This is a string";
int r1 = [ht rangeOfString:@"is"].location;
int r2 = [ht rangeOfString:@"is a"].location;
int r3 = [ht rangeOfString:@"isnt"].location;
NSLog(@"r1=%d, r2=%d, r3=%d, that's all\n",r1,r2,r3);
}
which prints:
Program loaded.
run
[Switching to process 21098]
Running…
2011-01-14 20:45:25.192 DELETEME[21098:a0b] r1=2, r2=5, r3=-1, that's all
running this in XCode, Mac OS... should be straightahead!
精彩评论