Reputation: 11227
How to add NSMutableArray
as an object(i) for another NSMutableArray
My code is:
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
Here i need to add tempImages as object of (i) for yearImages..
I need result as like follows:
[yearImages objectAtIndex:i];// result need as arrayobjects here
Upvotes: 0
Views: 203
Reputation:
Its very simple.
Replace [yearImages objectAtIndex:i]
into [yearImages addObject:tempImages.copy]
Now see full code:
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]);
Upvotes: 1
Reputation: 2966
You are removing the objects from tempImages after you add it, so the result will be an array of empty arrays. You should add a copy of tempImages instead:
[yearImages addObject:tempImages.copy]
(or a mutableCopy if you require that)
Upvotes: 1
Reputation: 14169
Does this even compile? k
in [yearImages insertObject:tempImages atIndex:k]
is not declared at all.
What error are you getting?
In order to simplify your code, you could get rid of the indices using this code.
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];
}
Upvotes: 1