Viktor De Bock
Viktor De Bock

Reputation: 139

Adding objects to NSMutableArray won't work

I'm trying to make an array(shopImages3) with arrays which hold 3 objects by using a temporary array(tempArray). The big array which contains all the strings to start with is shopImages.

The big array contains 6 strings at this moment. This means it comes 2 times in the

" if((i == 2) || (i == 5) || (i == 8) || (i == 11) || (i == 14) || (i == 18)) "

statement. This works fine. But the NSLogs show the arrays are null.

How can I fill the arrays correctly?

2014-08-28 14:01:52.575 Jump Game[3622:60b] this is temp array (null)
2014-08-28 14:01:52.575 Jump Game[3622:60b] this is shopimages array (null)
2014-08-28 14:01:52.576 Jump Game[3622:60b] this is temp array (null)
2014-08-28 14:01:52.576 Jump Game[3622:60b] this is shopimages array (null)

THE CODE STARTS FROM HERE

@interface ShopCollectionViewController ()
{
    NSArray *shopImages;

    NSMutableArray *shopImages3;
    NSMutableArray *tempArray;
}

........

for ( int i = 0; i < [shopImages count]; i++)
{

    [tempArray addObject: shopImages[i]];

    if((i == 2) || (i == 5) || (i == 8) || (i == 11) || (i == 14) || (i == 18))
    {
        NSLog(@"this is temp array %@", tempArray);
        [shopImages3 addObject:tempArray];
        NSLog(@"this is shopimages array %@", shopImages3);
        [tempArray removeAllObjects];
    }
}

Upvotes: 0

Views: 66

Answers (4)

Dreaming In Binary
Dreaming In Binary

Reputation: 1287

A few thoughts:

From your description, it sounds like this would be much better suited for an NSDictionary with the keys being your conditions (i.e. 2,5,7,11,14,18).

As for the null issue, I don't see where your collections are being initialized. You need to that first, unless they are being lazy loaded in the getter if they are properties.

NSMutableArray *mAr = [NSMutableArray new];

or

NSMutableArray *mAr = @[obj1,obj2,nil];

One last thing for syntactic sugar, you can put those conditions in an NSSet and shorten your if conditional.

 NSSet *set = [[NSSet alloc] initWithObjects:[NSNumber numberWithInt:1], nil]; //etc

 if([set containsObject:[NSNumber numberWithInt:i]])

Upvotes: 1

Ilesh P
Ilesh P

Reputation: 4036

you First alloc NSMutableArray like this

tempArray=[[NSMutableArray alloc]init];

Upvotes: 0

Ty Lertwichaiworawit
Ty Lertwichaiworawit

Reputation: 2950

Did you forget the to init you arrays ? Your code should look like this:

    tempArray = [NSMutableArray new];
    shopImages3 = [NSMutableArray new];
    shopImages = [NSMutableArray new];

for ( int i = 0; i < [shopImages count]; i++)
{

    [tempArray addObject: shopImages[i]];

    if((i == 2) || (i == 5) || (i == 8) || (i == 11) || (i == 14) || (i == 18))
    {
        NSLog(@"this is temp array %@", tempArray);
        [shopImages3 addObject:tempArray];
        NSLog(@"this is shopimages array %@", shopImages3);
        [tempArray removeAllObjects];
    }
}

Upvotes: 0

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

Looks like you do not initialise the arrays. In your init you need to

tempArray = [NSMutableArray new];

Upvotes: 2

Related Questions