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.

Phil
  • 2,995
  • 6
  • 40
  • 67
  • 3
    possible duplicate of [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar), [NSMutableArray addObject: not affecting count](http://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count), and [NSMutableArray addObject: not working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working), the links to which [I already gave you](http://stackoverflow.com/questions/11401368/storing-pointers-to-objects-in-array-in-objective-c#comment15035378_11401368). – jscs Jul 09 '12 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. – Phil Jul 09 '12 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. – Tommy Jul 09 '12 at 22:27

2 Answers2

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]);
Brian Walker
  • 8,658
  • 2
  • 33
  • 35
  • I've got this in the .m file synthesize TerrainHexArray; and this in the .h file @property NSMutableArray *TerrainHexArray; – Phil Jul 09 '12 at 22:03
  • 2
    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. – Brian Walker Jul 09 '12 at 22:44
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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151