2012-08-14 28 views
-3

我已經定義了三個字符串作爲s1 =「@ $ 100」,s2 =「@ $ 50」,s3 =「@ $ 150」。現在我想要在S4 =「@ $ 300」中存儲三個字符串的總數,其中所有內部函數方法都以單一方法在目標c代碼中執行?想要代碼在目標c

+3

[你有什麼嘗試?](http:// mattgem mell.com/2008/12/08/what-have-you-tried/) – 2012-08-14 13:51:44

+0

聽起來像一個家庭作業問題。 – Abizern 2012-08-14 13:55:15

+2

s1 =「@ $ 100」不正確,不適用於NSString,反正 – meronix 2012-08-14 13:58:37

回答

0

首先,對於字符串文字賦值,您需要@""而不是"@"。其次,要加上整數,你應該使用ints。然後您可以使用stringWithFormat:將其轉換爲字符串。但是,既然你是從字符串開始的,只需將它們轉換爲整數,然後將它們加起來即可。

sFourString = [S4 intValue]; //Do this for all of your strings 

添加您的整數在一起:

resultingValue = sFourString + allOtherInts; 

,然後再轉換resultingValue回字符串:

NSString *resultString = [NSString stringWithFormat:@"%i", resultingValue]; 

所以把他們放在一起在一個大的語句:

NSString *completeString = [NSString stringWithFormat:@"%i", [S4 intValue] + [anotherString intValue]];