2011-01-29 56 views
0

我用下面的語句到到NSString的轉換(用查找/替換)INT到的NSString問題

curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:(tmpCurrYearInt-1)]]; 

我宣佈

int tmpYrVal; 
NSMutableString *tmp_dt,*curr_rep_date; 

但該計劃似乎要崩潰和調試器沒有提供任何提示。

有人可以幫我解決這個問題,什麼是正確的用法。

回答

1

這裏有很多問題。

首先,sringByReplacingOcurrancesOfString:withString:期望NSString s作爲參數,而不是int s。這就是它崩潰的原因。該方法試圖發送消息到原始類型,而不是對象。

其次,您需要爲stringWithFormat:方法使用正確的格式字符串。這與NSLog的工作方式相同。

格式字符串可能看起來像@"some text %d"。然後將會使用逗號分隔的值列表來代替%佔位符。

例子:

[NSString stringWithFormat:@"%d", myIntValue]; 

將有效地把你的INT轉換爲字符串,因爲它創造了使用您的INT格式的新字符串。

1

您調用了不帶格式字符串的stringWithFormat - Method。 [NSString stringWithFormat:@「%i」,(tmpCurrYearInt-1)]應該可以解決你的問題。

0

你缺少格式


curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:@"%d", (tmpCurrYearInt-1)]]; 

+0

我用上面的行代替。該程序仍在崩潰。 – testndtv 2011-01-29 14:24:48

0

基本的int的NSString轉換是這樣的:

NSString* s = [NSString stringWithFormat:@"%d", intNumber];