appendString从iOS中的阵列到阵列
问题描述:
我有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]];
}
答
有很多方法可以做到这一点,但我可能会使用这样的:
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
精湛的工作,请参阅编辑后的答案! –
答
这是做(未经测试的代码,直接在我心里)的一种方式:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
int i = 0;
for (NSString *what in itemArray) {
[newAray addObject:[NSString stringWithFormat:@"%@: (%d)", what, [quantityArray objectAtIndex:i]]];
i++
}
答
这不应该是很难...代码在我心里。所以可能会有错误;)。
int i = 0;
int len = products.count;
for(i = 0; i < len; i++) {
[NSString stringWithFormat:@"%@ QTY: (%d)", [products objectAtIndex:i], ([quantities objectAtIndex:i]]
}
注意:您必须检查,这两个数组都有相同的计数。所以最好也是在前面插入断言来检查该条件。
发布您的代码,请 –
Hi Kai对不起,我忘了添加代码傻了我。任何方式你去。 theProducts和theQuantity是2 NSMutableArrays这是产生的产品名称与整个数量数组添加到最后,即足球数量:(12,5,17),我需要它来打破在索引的数量,所以足球是12等等等等! –