0

I'm trying to store 25 objects in an array

for (int iy=0; iy<5; iy++) {
        for (int ix=0; ix<5; ix++) {

            TerrainHex *myObject = [[TerrainHex alloc] initWithName:(@"grassHex instance 10000") width:mGameWidth height:mGameHeight indexX:ix indexY:iy];
            myObject.myImage.y += 100;

            [TerrainHexArray addObject:myObject];

            [self addChild:(id)myObject.myImage];
        }
    }
    NSLog(@"Terrain array: %u", [TerrainHexArray count]);

The log is coming back as zero though.

In the .h file I have

@property NSMutableArray *TerrainHexArray;

And in the .m file I have..

@synthesize TerrainHexArray;

I just tried what someone suggested below, which is..

NSMutableArray *TerrainHexArray = [[NSMutableArray] alloc] init];

But it's just giving me a warning saying expected identifier.

3
  • 3
    possible duplicate of Cannot add items to an NSMutableArray ivar, NSMutableArray addObject: not affecting count, and NSMutableArray addObject: not working, the links to which I already gave you. Commented Jul 9, 2012 at 21:21
  • It wouldn't let me reply to any of the posts in the last question, so I had to start another one up, also this seems to be a separate issue. Commented Jul 9, 2012 at 22:02
  • 1
    @Phil regardless, I'll give you 100:1 that the problem is just that you haven't actually created an array as Josh posted. Your attempt to create one is also broken; you're declaring a local variable with the same name as an instance variable, so the local variable masks the instance variable. Oh, and you've accidentally typed an extra ']' in your post but I'm sure that's neither here nor there. Commented Jul 9, 2012 at 22:27

2 Answers 2

0

It's almost certain that TerrainHexArray does not exist when you're doing the addObject calls and the NSLog. You say you tried adding the alloc/init after someone suggested it, which indicates you don't understand object management in Objective-C.

I'd suggest you step back, find a book on Objective-C, and read at least the first few chapters (up through the discussion of alloc/init et al) before you attempt any more coding.

Incidentally, it's standard C++/Objective-C coding practice (except in Microsoft) to use identifiers with a leading lower case character for instance names, reserving leading caps for types/class names.

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

Comments

0

What is TerrainHexArray? It looks like a class name, not an instance of an array. If you create a mutable array, then you can add the items to the array.

NSMutableArray *hexArray = [[NSMutableArray] alloc] init];
for (int iy=0; iy<5; iy++) {
        for (int ix=0; ix<5; ix++) {

            TerrainHex *myObject = [[TerrainHex alloc] initWithName:(@"grassHex instance 10000") width:mGameWidth height:mGameHeight indexX:ix indexY:iy];
            myObject.myImage.y += 100;

            [hexArray addObject:myObject];

            [self addChild:(id)myObject.myImage];
        }
}
NSLog(@"Terrain array: %u", [hexArray count]);

2 Comments

I've got this in the .m file synthesize TerrainHexArray; and this in the .h file @property NSMutableArray *TerrainHexArray;
Where are you creating the array? If is it array instance is nil then the addObject will do nothing and the count will always return 0.

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.