2012-05-02 43 views
0

我有2個獨立的Plist NSMutableArrays包含字符串。appendString從iOS中的陣列到陣列

第一陣列的例子包含以下產品:

足球, 羽毛球拍, 網球,

第二陣列包含上述產品的量:

12, 5, 17,

我想要實現的是將數量添加到產品名稱的末尾,即如下所示:

足球QTY:(12), 羽毛球拍QTY:(5), 網球QTY:(17),

的NSMutableString *都是EmailString = [字符串的NSMutableString]。

for(int i = 0; i < [theProducts count]; i++) { 

    NSString *str = [theProducts objectAtIndex:i]; 
    [emailString appendString:[NSString stringWithFormat:@"<br /> %@: QTY: %@", str, theQuantity]]; 
} 

任何建議最好的方式來做到這一點將不勝感激。我已經看了追加字符串的方法和遍歷字符串添加到上述陣列中的字符串,但是我似乎無法得到這個:-(

感謝工作爲尋找

更新!

這對我的作品

NSMutableString *emailString = [NSMutableString string]; 

for(int i = 0; i < [theProducts count]; i++) { 

    NSString *result = [NSString stringWithFormat: @"<br /> %@ QTY: (%d)", [theProducts objectAtIndex: i], [[theQuantity objectAtIndex: i] intValue], nil]; 
    [emailString appendString:[NSString stringWithFormat:@"%@", result]]; 

} 
+0

發佈您的代碼,請 –

+0

Hi Kai對不起,我忘了添加代碼傻了我。任何方式你去。 theProducts和theQuantity是2 NSMutableArrays這是產生的產品名稱與整個數量數組添加到最後,即足球數量:(12,5,17),我需要它來打破在索引的數量,所以足球是12等等等等! –

回答

0

有很多方法可以做到這一點,但我可能會使用這樣的:

NSString *result = [NSString stringWithFormat: @"%@ QTY: (%d)", name, quantity, nil]; 

你會取代你的名字符串數組,並在任何方式,第二陣列搶量說得通。如果第一陣列叫名字,第二個是所謂的數量NSNumber的對象的數組,該行可能看起來像這樣:

NSString *result = [NSString stringWithFormat: @"%@ QTY: (%d)", [names objectAtIndex: i], [[quantities objectAtIndex: i] intValue], nil]; 
+0

精湛的工作,請參閱編輯後的答案! –

0

這是做(未經測試的代碼,直接在我心裏)的一種方式:

NSMutableArray *newArray = [[NSMutableArray alloc] init]; 
int i = 0; 

for (NSString *what in itemArray) { 
    [newAray addObject:[NSString stringWithFormat:@"%@: (%d)", what, [quantityArray objectAtIndex:i]]]; 
    i++ 
} 
0

這不應該是很難...代碼在我心裏。所以可能會有錯誤;)。

int i = 0; 
int len = products.count; 

for(i = 0; i < len; i++) { 
    [NSString stringWithFormat:@"%@ QTY: (%d)", [products objectAtIndex:i], ([quantities objectAtIndex:i]] 
} 

注意:您必須檢查,這兩個數組都有相同的計數。所以最好也是在前面插入斷言來檢查該條件。