2010-11-18 66 views
1

我有一個int currentFontSize,我想與存儲在C數組中的NSString比較,這裏是代碼。int stringWithFormat到NSString返回值

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; 
} 

橫幅部分是,val給我返回值1879919632,這使我的比較失敗。奇怪的部分是,當我硬編碼的NSString作爲VAL2 = @「22」,比較成爲成功的地方VAL2是:4240,這是一樣的:

NSLog(@"fontSizeNAme:%d",fontSizeName[i]); 

這裏是我的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 

任何人都可以善意解釋我爲什麼?爲什麼我投了@「22」NString和一個形式的int返回一個不同的值?????謝謝!

回答

6

在Objective-C中,您不能僅比較包含整數的字符串表示形式的intNSString*。在兩者之間轉換,你需要做的

NSString* [email protected]"11"; 
int i=[s intValue]; // i now contains 11 

此外,在NSLog%規範確實指定要如何對象來解釋。您需要指定與對象類型匹配的正確的%。否則,它只是顯示你垃圾。所以,無論是

NSLog(@"the current font Value1 is %i", val); 
NSLog(@"the current font Value2 is %i",val2); 

是非法的,因爲valval2NSString*。相反,你需要做的

NSLog(@"the current font Value1 is %@", val); 
NSLog(@"the current font Value2 is %@",val2); 

NSLog(@"the current font Value1 is %i", [val intValue]); 
NSLog(@"the current font Value2 is %i",[val2 intValue]); 

你不能或者通過==比較兩個NSString*秒。您需要使用[aString isEqualTo:anotherString]

1

我同意@Yuji,在目標C中,你不能試圖比較字符串和整數。您可能最終會在某處放入系統緩衝區的通配符。或者如果你有調試方法,你可能會得到一個out of range exception

+0

實際上,程序會在'int'和指向Obj-C對象的指針之間進行比較,這是完全有效的,它只會給你非感性的結果。 – 2010-11-18 11:48:37

+0

我相信這就是我所說的。 – 2010-11-18 12:08:19

+0

@邁克爾艾金斯:無論你相信什麼,這不是你所說的。你說過:「你可能最終會遇到一個運行在系統緩衝區中的野號......或者超出範圍的異常」。如果你比較一個指向int的指針,那麼這兩者都不會發生。 – JeremyP 2010-11-18 14:48:56