我有很少NSMutableArrays
,例如arr1,arr2,arr3,arr4。如何将所有NSMutableArrays
合并在一起?或者有多少方法可以多次追加到一个数组中?
示例:
arr = [[NSMutableArray alloc] init];
//first loop
for(condition){
[arr addObject:firstLoopValue];
}
//second loop
for(condition){
[arr addObject:secondLoopValue];
}
//third loop
for(condition){
[arr addObject:thirdLoopValue];
}
最后的结果{firstLoopValue...secondLoopValue...thirdLoopValue}
我是iOS的新手,为什么不确定它是否可行。提前致谢
修改 我尝试过以下两种方式:
rCodeArr = [[NSMutableArray alloc] init];
rDateArr = [[NSMutableArray alloc] init];
rAmountArr = [[NSMutableArray alloc] init];
jCodeArr = [[NSMutableArray alloc] init];
jDateArr = [[NSMutableArray alloc] init];
jAmountArr = [[NSMutableArray alloc] init];
jBalanceArr = [[NSMutableArray alloc] init];
NSArray *component = [journal getComponents:custCode];
for(int i = 0; i < [component count]; i++){
list = [component objectAtIndex:i];
NSArray *timestamp = [list.jDate componentsSeparatedByString:@" "];
NSString *splitdate = timestamp[0];
[jCodeArr addObject:list.jCode];
[jDateArr addObject:splitdate];
[jAmountArr addObject:list.jAmount];
[jBalanceArr addObject:list.jOutstanding];
}
NSArray *rcom = [receipt getComponents:custCode];
for(int j = 0; j < [rcom count]; j++){
rList = [rcom objectAtIndex:j];
NSArray *rtime = [rList.rDate componentsSeparatedByString:@" "];
NSString *rsplit = rtime[0];
[rCodeArr addObject:rList.rCode];
[rDateArr addObject:rsplit];
[rAmountArr addObject:rList.rAmount];
[rBalanceArr addObject:@"N/A"];
}
lblOneArray = [NSMutableArray arrayWithArray:rCodeArr];
[lblOneArray addObjectsFromArray:jCodeArr];
lblTwoArray = [NSMutableArray arrayWithArray:rDateArr];
[lblTwoArray addObjectsFromArray:jDateArr];
lblThreeArray = [NSMutableArray arrayWithArray:rAmountArr];
[lblThreeArray addObjectsFromArray:jAmountArr];
lblFourArray = [NSMutableArray arrayWithArray:jBalanceArr];
[lblFourArray addObjectsFromArray:rBalanceArr];
和
lblOneArray = [[NSMutableArray alloc] init];
lblTwoArray = [[NSMutableArray alloc] init];
lblThreeArray = [[NSMutableArray alloc] init];
lblFourArray = [[NSMutableArray alloc] init];
[lblOneArray addObjectsFromArray:rCodeArr];
[lblOneArray addObjectsFromArray:jCodeArr];
[lblTwoArray addObjectsFromArray:rDateArr];
[lblTwoArray addObjectsFromArray:jDateArr];
[lblThreeArray addObjectsFromArray:jAmountArr];
[lblThreeArray addObjectsFromArray:jAmountArr];
[lblFourArray addObjectsFromArray:rBalanceArr];
[lblFourArray addObjectsFromArray:jBalanceArr];
但是它仍然在崩溃消息*****由于未捕获的异常终止应用程序&#39; NSRangeException&#39;,原因:&#39; * - [__ NSArrayM objectAtIndex:] :索引5超出界限[0 .. 4]&#39;
注意所有这些都是NSMutableArray
答案 0 :(得分:2)
您可以通过添加如下所示的所有数组来创建最终数组
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<SomeObject>> someMethod(HttpServletRequest request, @RequestParam(required = false) MultiValueMap<String, String> params) {
String url = request.getRequestURL().toString() + (request.getQueryString() == null ? "" : "?" + request.getQueryString());
}
它没有崩溃,因为我检查了以下代码
NSMutableArray *newArray = [[NSMutableArray alloc] init];
[newArray addObjectsFromArray:firstArray];
[newArray addObjectsFromArray:secondArray];
答案 1 :(得分:2)
要组合多个数组,您可以使用第一个数组的元素初始化数组,然后从中添加元素:
NSMutableArray* arr1 = [NSMutableArray arrayWithArray: firstLoopValue];
[arr1 addObjectsFromArray: secondLoopValue];
[arr1 addObjectsFromArray: thirdLoopValue];
[arr1 addObjectsFromArray: forthLoopValue];
答案 2 :(得分:1)
只做
arr = [[NSMutableArray alloc] init];
[arr addObjectsFromArray:anArray1];
[arr addObjectsFromArray:anArray2];
[arr addObjectsFromArray:anArray3];