Monday, December 26, 2011

Comparing NSString AND NSNumber in XCode

For the beginners, it is a bit uncommon string comparing syntax. I faced the same problem at the beginning of iPhone programming. It is not complex but jut you have to know the syntax, that’s it.
Get the string from a nsdata type and compare it with other string.
NSString comparison--
 NSString *txt = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSString *txtComp = @"Value1";
if ([txt isEqualToString:txtComp]) {
lblVal.text = txt;
}
Note: [The “==” operator will compare the reference to your string literal and so never return true.]
Now, to compare NSNumeric -
if([numValue isEqualToNumber:[NSNumber numberWithInt:7]])
 yourFunction();
Or,
If([myNumber intValue] == 7)
Now , compare String with Number-
Just convert Number to string and do compare.
Post a Comment