I have an int currentFontSize where I wanna compare with NSString stored in an C array, here is the code.
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
static NSString* fontSizeName[] =
{
@"14",
@"18",
@"22",
@"26",
@"30",
@"34",
};
int fontSizeSegmentID;
int currentFontSize = 22;
NSString *val = [NSString stringWithFormat: @"%i", currentFontSize];
NSString *val2 = @"22";
NSLog(@"the current font size is %i", currentFontSize);
NSLog(@"the current font Value1 is %i", val);
NSLog(@"the current font Value2 is %i",val2);
int i;
for (i = 0; i < 6; i++) {
NSLog(@"fontSizeNAme:%d",fontSizeName[i]);
NSLog(@"val:%d",val);
if (fontSizeName[i] == val) {
NSLog(@"They are equal");
fontSizeSegmentID = i;
break;
}
NSLog(@"the fontSizeSegmentID is: %i" , fontSizeSegmentID);
}
[pool drain];
return 0;
}
the strage part is , val give me a return value of 1879919632, which make my comparison failed. The strange part is when I hardcode the NSString as val2 = @”22″, the comparison become successful where val2 is: 4240, it’s the same as:
NSLog(@"fontSizeNAme:%d",fontSizeName[i]);
here is my return in NSLog:
2010-11-18 16:56:52.784 testINT[26034:a0f] the current font size is 22
2010-11-18 16:56:52.786 testINT[26034:a0f] the current font Value1 is 1879919632
2010-11-18 16:56:52.787 testINT[26034:a0f] the current font Value2 is 4240
2010-11-18 16:56:52.788 testINT[26034:a0f] fontSizeNAme:4176
2010-11-18 16:56:52.788 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.788 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.789 testINT[26034:a0f] fontSizeNAme:4208
2010-11-18 16:56:52.789 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.790 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.790 testINT[26034:a0f] fontSizeNAme:4240
2010-11-18 16:56:52.790 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.791 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.791 testINT[26034:a0f] fontSizeNAme:4272
2010-11-18 16:56:52.791 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.792 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.792 testINT[26034:a0f] fontSizeNAme:4304
2010-11-18 16:56:52.792 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.793 testINT[26034:a0f] the fontSizeSegmentID is: 32767
2010-11-18 16:56:52.793 testINT[26034:a0f] fontSizeNAme:4336
2010-11-18 16:56:52.794 testINT[26034:a0f] val:1879919632
2010-11-18 16:56:52.794 testINT[26034:a0f] the fontSizeSegmentID is: 32767
Can anybody kindly explain me why? why I cast the @”22″ NString and the one form int returns a different value????? Thanks!
In Objective-C, you can’t just compare an
intandNSString*which contains a string representation of an integer. To convert between the two, you need to doAlso, the
%specification inNSLogdoes not specify how you want the object to be interpreted. You need to specify the correct%thing matching with the type of the object. Otherwise it just shows you junk. So, bothare illegal, because
valandval2areNSString*. Instead you need to door
You can’t compare two
NSString*s by==either. You need to use[aString isEqualTo:anotherString].