0

Here's my code

-(NSMutableDictionary*)checkArrayForObject:(NSArray *)array withX:(int)mapX withY:(int)mapY withWith:(int)w withHeight:(int)h
{
    NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] init];

    int cellWidth2 = w;
    int cellHeight2 = h;

    NSMutableArray *temp = [[NSMutableArray alloc ] initWithCapacity:(cellWidth2*cellHeight2)];

    //lets fill the array with something
    for (int iy=0; iy<cellWidth2; iy++) {
        for (int ix=0; ix<cellHeight2; ix++) {
            [temp addObject:[NSNull null]];
        }
    }


    Boolean overObjectYES = YES;
    Boolean overObjectNo = NO;

    [tempDictionary setObject:[NSNumber numberWithBool:overObjectNo] forKey:@"collision"];
    [tempDictionary setObject:temp forKey:@"regionArray"];

    NSLog(@"mapx, %d", mapX);
    NSLog(@"mapy, %d", mapY);
    NSLog(@"cell width, %d", cellWidth2);
    NSLog(@"cell height, %d", cellHeight2);


    int xx = 0;
    int yy = 0;

     NSLog(@"DO CHECK");

    for (int iy=mapY; iy<(mapY + cellHeight2); iy++) {

        xx = 0;

        for (int ix=mapX; ix<(mapX + cellWidth2); ix++) {

            //make index
            int ii = (iy*mapWidth)+ix;

             NSLog(@"array index, %d", ii);

            if([array objectAtIndex:ii] != [NSNull null])
            {

                NSLog(@"OVERLAPPING OBJECT");

                [tempDictionary setObject:[NSNumber numberWithBool:overObjectYES] forKey:@"collision"];

                //get object in real array
                NSMutableDictionary *obj = [array objectAtIndex:ii];

                //place in temp in same relative position

                //make index
                int ii2 = (yy*cellWidth2)+xx;

                if(ii2 < [temp count])
                {
                    [temp insertObject:obj atIndex:ii2];
                }
            }

            xx++;
        }

        yy++;
    }
    return tempDictionary;
}

And in another method I call this method, but then I'm trying to get access to the object that has been stored in the array, but I can't seem to access it, or the above code (the insertObject:obj line) doesn't seem to insert anything.

This the code I'm using to access the above...

 NSMutableDictionary *regionDictionary = [sharedInstance checkArrayForObject:sharedInstance.playerObjects  withX:sharedInstance.mapTileX withY:sharedInstance.mapTileY withWith:1 withHeight:1];

            Boolean overlap = [[regionDictionary objectForKey:@"collision"] boolValue];

            if(overlap == YES)
            {
               [self setupInfoMsg:[[regionDictionary objectForKey:@"region"]objectAtIndex:0]];
            }

When I step through that code and go into setupInfoMSg, the parameter = 0.

I'm new to Obj-c so it's probably a simple mistake.

Simon Germain
  • 6,834
  • 1
  • 27
  • 42
Phil
  • 2,995
  • 6
  • 40
  • 67
  • You haven't allocated `array`. Possible duplicate of [Having trouble adding objects to NSMutableArray](http://stackoverflow.com/q/851926) [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/q/7125326), [\[NSMutableArray addObject:\] not affecting count](http://stackoverflow.com/q/3683761), [\[NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – jscs Apr 01 '13 at 00:55
  • `array` is passed as a parameter, the signature is just not declared properly. – Simon Germain Apr 01 '13 at 00:58
  • That's not the problem, in the first method NSMutableDictionary *obj = [array objectAtIndex:ii]; works fine, obj does = the object it should, it's just it doesn't seem to be inserted into the temp array...ooh do I need to declare the temp array outside of this method? for it to be access by the other method? – Phil Apr 01 '13 at 01:04
  • You insert an element into the dictionary using `[tempDictionary setObject:temp forKey:@"regionArray"];`, and then read an element using `[regionDictionary objectForKey:@"region"]`. Perhaps you intended to use the same key (either region or regionArray) in these two places? – Mike Mertsock Apr 01 '13 at 02:06
  • 1
    *sigh* yes that was it, it's alway the typo's that get you, thanks! – Phil Apr 01 '13 at 02:10
  • The best moment to start using shared constants as keys. – A-Live Apr 01 '13 at 05:17
  • How do I do shared constants? just use the define statement? – Phil Apr 02 '13 at 00:01

0 Answers0