從閱讀這個問題很多,我想我可能會明白你想要什麼。
的出發點似乎是:
NSLog(@"add string is: %@",addString);// result is: 45,1
而且目前的終點是:
NSLog(@"myInt is: %d",myInt);// result is: 45
但似乎你仍然想打印出45,1
我猜測這是你有一個2字符串[@「45」,@「1」]的數組,稱爲arrayyyy
,你想要打印出兩個值作爲整數。如果是這樣,那麼我想你想的是:
NSInteger myInt1 = [[arrayyyy objectAtIndex:0] intValue];
NSInteger myInt2 = [[arrayyyy objectAtIndex:1] intValue];
NSLog(@"add string is: %d,%d",myInt1,myInt2);
注意這將有NSRangeException可怕的崩潰,如果沒有在陣列中至少兩個字符串。因此,在最起碼你應該做的:
NSInteger myInt1 = -1;
NSInteger myInt2 = -1;
if ([arrayyyy length] >0) myInt1 = [[arrayyyy objectAtIndex:0] intValue];
if ([arrayyyy length] >1) myInt2 = [[arrayyyy objectAtIndex:1] intValue];
NSLog(@"add string is: %d,%d",myInt1,myInt2);
但即便如此糟糕,因爲它假定的-1
的防護值將不會出現在實際的數據。
以及,如果結果是45,其轉換爲int。 [addString intValue]轉換爲int,[addString integerValue]轉換爲NSInteger。 – 2012-03-09 13:09:55
http://stackoverflow.com/questions/4791470/convert-nsstring-to-nsinteger – bdparrish 2012-03-09 13:09:59
@ user993223你是什麼意思「將字符串轉換爲整數」?你想達到什麼結果? – 2012-03-09 13:10:15