Can get members, but not count of NSMutableArray

Posted by Curyous on Stack Overflow See other posts from Stack Overflow or by Curyous
Published on 2010-06-12T09:31:13Z Indexed on 2010/06/12 9:43 UTC
Read the original article Hit count: 219

I'm filling an NSMutableArray from a CoreData call. I can get the first object, but when I try to get the count, the app crashes with Program received signal: “EXC_BAD_ACCESS”. How can I get the count?

Here's the relevant code - I've put a comment on the line where it crashes.

- (void)viewDidLoad {
[super viewDidLoad];

managedObjectContext = [[MySingleton sharedInstance] managedObjectContext];

if (managedObjectContext != nil) {
    charactersRequest = [[NSFetchRequest alloc] init];
    charactersEntity = [NSEntityDescription entityForName:@"Character" inManagedObjectContext:managedObjectContext];
    [charactersEntity retain];
    [charactersRequest setEntity:charactersEntity];
    [charactersRequest retain];

    NSError *error;
    characters = [[managedObjectContext executeFetchRequest:charactersRequest error:&error] mutableCopy];
    if (characters == nil) {
        NSLog(@"Did not get results for characters: %@", error.localizedDescription);
    }
    else {          
        [characters retain];            
        NSLog(@"Found some character(s).");
        Character* character = (Character *)[characters objectAtIndex:0];
        NSLog(@"Name of first one: %@", character.name);
        NSLog(@"Found %@ character(s).", characters.count);  // Crashes on this line with - Program received signal:  “EXC_BAD_ACCESS”.
    }
}

}

And previous declarations from the header file:

@interface CrowdViewController : UITableViewController {
NSManagedObjectContext *managedObjectContext;
NSFetchRequest *charactersRequest;
NSEntityDescription *charactersEntity;
NSMutableArray *characters;

}

I'm a bit perplexed and would really appreciate finding out what is going on.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about memory-management