如何添加NSMutableArray作为objectAtIndex NSMutableArray
如何添加NSMutableArray
作为对象(i)的另一个NSMutableArray
我的代码是:
yearImages = [[NSMutableArray alloc]init]; tempImages = [[NSMutableArray alloc]init]; for(int i =0; i< [yearImagesName count]; i++) { for(int j=0; j<[totalImagesName count]; j++) { if ([[totalImagesName objectAtIndex:j] rangeOfString:[yearImagesName objectAtIndex:i]].location != NSNotFound) { [tempImages addObject:[totalImagesName objectAtIndex:j]]; } } [yearImages addObject:tempImages]; [tempImages removeAllObjects]; } NSLog(@"\n\n year%@",[yearImages objectAtIndex:0]); // getting null result
-
在这里,我需要添加tempImages作为(i)对象的yearImages对象..
-
我需要如下的结果:
[yearImages objectAtIndex:i];// result need as arrayobjects here
添加它之后,您将从tempImages中删除对象,所以结果将是一个空数组的数组。 你应该添加一个tempImages的副本: [yearImages addObject:tempImages.copy]
(或者一个mutableCopy,如果你需要的话)
这甚至编译? 在[yearImages insertObject:tempImages atIndex:k]
中的[yearImages insertObject:tempImages atIndex:k]
根本不被声明。
你遇到了什么错误?
为了简化你的代码,你可以使用这段代码去掉索引。
yearImages = [[NSMutableArray alloc]init]; tempImages = [[NSMutableArray alloc]init]; for(NSString *yearImageName in yearImagesName) { for(NSString *totalImageName in totalImagesName) { if ([totalImageName rangeOfString:yearImageName].location != NSNotFound) { [tempImages addObject:totalImageName]; } } [yearImages addObject:tempImages]; [tempImages removeAllObjects]; }
它非常简单。
将[yearImages objectAtIndex:i]
replace为[yearImages addObject:tempImages.copy]
现在看完整的代码:
for(int i =0; i< [yearImagesName count]; i++) { for(int j=0; j<[totalImagesName count]; j++) { if (// Your Conditon) { [tempImages addObject:[totalImagesName objectAtIndex:j]]; } } [yearImages addObject:tempImages.copy]; // each array stored as an object [tempImages removeAllObjects]; } NSLog(@"\n\n year%@",[yearImages objectAtIndex:0]);