Note that there are some explanatory texts on larger screens.

plurals
  1. POint stringWithFormat to NSString return value
    primarykey
    data
    text
    <p>I have an int currentFontSize where I wanna compare with NSString stored in an C array, here is the code.</p> <pre><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 &lt; 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; } </code></pre> <p>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:</p> <pre><code>NSLog(@"fontSizeNAme:%d",fontSizeName[i]); </code></pre> <p>here is my return in NSLog:</p> <pre><code>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 </code></pre> <p>Can anybody kindly explain me why? why I cast the @"22" NString and the one form int returns a different value????? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload