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.

7
  • You haven't allocated array. Possible duplicate of Having trouble adding objects to NSMutableArray Cannot add items to an NSMutableArray ivar, [NSMutableArray addObject:] not affecting count, [NSMutableArray addObject:] not working Commented Apr 1, 2013 at 0:55
  • array is passed as a parameter, the signature is just not declared properly. Commented Apr 1, 2013 at 0: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? Commented Apr 1, 2013 at 1: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? Commented Apr 1, 2013 at 2:06
  • 1
    sigh yes that was it, it's alway the typo's that get you, thanks! Commented Apr 1, 2013 at 2:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.