0

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
7
  • what error r u getting? Post the actual compiler error... Commented Nov 28, 2013 at 9:56
  • try to change [yearImages insertObject:tempImages atIndex:i]; to [yearImages addObject:tempImages]; Commented Nov 28, 2013 at 10:03
  • @Chancy: These both will be same.. Commented Nov 28, 2013 at 10:08
  • @Chancy: am changed but still getting result null Commented Nov 28, 2013 at 10:09
  • null plz check have you initialised every object in your code? Commented Nov 28, 2013 at 10:10

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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];
}

2 Comments

i need to add temp1 array as[yearImages objectAtIndex:0], temp2 array as[yearImages objectAtIndex:1],
there is no error in if loop.. i got temp array separately each time. but i need whole temp data in yearimage array
1

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.