0

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];
    }
}
1
  • Are u alloc array or not.? Commented Aug 28, 2014 at 12:06

4 Answers 4

2

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

tempArray = [NSMutableArray new];
Sign up to request clarification or add additional context in comments.

5 Comments

When I NSLog the *shopImages3 inside the for loop, it shows the correct data. But when I access the array outside the loop it's empty. How can I fix this?
@ViktorDeBock You say that NSLog for shopImages3 if repeated after the loops shows empty array? How do you initialise shopImages3 and tempArray?
In the ViewDidLoad method I wrote this: tempArray = [NSMutableArray new]; shopImages3 = [NSMutableArray new];
It all looks good. Do more investigation - NSLog to be sure that array is empty etc.
The content of ARRAY is( ( ), ( ) ) So it still show 2 arrays but the content is empty.
1

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

Comments

0

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

Comments

0

you First alloc NSMutableArray like this

tempArray=[[NSMutableArray alloc]init];

1 Comment

Do you know, you can write fewer lines of code with [NSMutableArray new] :)

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.