Global NSMutableArray doesn't seem to be holding values

Posted by diatrevolo on Stack Overflow See other posts from Stack Overflow or by diatrevolo
Published on 2010-05-19T17:58:12Z Indexed on 2010/05/19 18:00 UTC
Read the original article Hit count: 201

I have a Cocos2D iPhone application that requires a set of CGRects overlaid on an image to detect touches within them. "Data" below is a class that holds values parsed from an XML file. "delegateEntries" is a NSMutableArray that contains several "data" objects, pulled from another NSMutableArray called "entries" that lives in the application delegate.

For some strange reason, I can get at these values without problems in the init function, but further down the class in question, I try to get at these values, and the application crashes without an error message (as an example, I put in the "ccTouchBegan" method which accessess this data through the "populateFieldsForTouchedItem" method.

Can anyone see why these values would not be accessible from other methods? No objects get released until dealloc. Thanks in advance!

@synthesize clicked, delegate, data, image, blurImage, normalImage, arrayOfRects, delegateEntries;
- (id)initWithTexture:(CCTexture2D *)aTexture {

    if( (self=[super initWithTexture:aTexture] )) {
        arrayOfRects = [[NSMutableArray alloc] init];
        delegateEntries = [[NSMutableArray alloc] init];
        delegate = (InteractivePIAppDelegate *)[[UIApplication sharedApplication] delegate];
        delegateEntries = [delegate entries];
        data = [delegateEntries objectAtIndex:0];
        NSLog(@"Assigning %@", [[delegateEntries objectAtIndex:0] backgroundImage]);
        NSLog(@"%@ is the string", [[data sections] objectAtIndex:0]);
        //CGRect rect;
        NSLog(@"Count of array is %i", [delegateEntries count]);

        //collect as many items as there are XML entries
        for(int i=0; i<[delegateEntries count]; i++) {
            if([[delegateEntries objectAtIndex:i] xPos]) {
                NSLog(@"Found %i items", i+1);
                [arrayOfRects addObject:[NSValue valueWithCGRect:CGRectMake([[[delegateEntries objectAtIndex:i] xPos] floatValue], [[[delegateEntries objectAtIndex:i] yPos] floatValue], [[[delegateEntries objectAtIndex:i] xBounds] floatValue], [[[delegateEntries objectAtIndex:i] yBounds] floatValue])]];
            } else {
                NSLog(@"Nothing");
            }
        }
        //remove the following once the NSMutableArray from above works (legacy)
        blurImage = [[NSString alloc] initWithString:[data backgroundBlur]];
        NSLog(@"5");
        normalImage = [[NSString alloc] initWithString:[data backgroundImage]];
        clicked = NO;
    }
    return self;    
}

And then:

- (void)populateFieldsForTouchedItem:(TouchedRect)touchInfo
{
    Data *touchDatum = [[Data alloc] init];
    touchDatum = [[self delegateEntries] objectAtIndex:touchInfo.recordNumber];
    NSLog(@"Assigning %@", [[[self delegateEntries] objectAtIndex:touchInfo.recordNumber] backgroundImage]);
    rect = [[arrayOfRects objectAtIndex:touchInfo.recordNumber] CGRectValue];
    image = [[NSString alloc] initWithString:[[touchDatum sections] objectAtIndex:0]];
    [touchDatum release];
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    TouchedRect touchInfo = [self containsTouchLocation:touch];
    NSLog(@"Information pertains to %i", touchInfo.recordNumber);
    if ( !touchInfo.touched && !clicked ) {  //needed since the touch location changes when zoomed
        NSLog(@"NOPE");
        return NO;
    }
    [self populateFieldsForTouchedItem:touchInfo];
    NSLog(@"YEP");
    return YES;
}

© Stack Overflow or respective owner

Related posts about cocoa-touch

Related posts about cocos2d-iphone