views:

64

answers:

2

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.

+2  A: 

Count is an integer so, you should have used %d when using it with NSLog

NSArray Reference

vodkhang
I've used `.count` in other places in my code and that works fine on other NSMutableArrays. The code above also crashes when I use `[characters count]`.
Curyous
Sorry, I know the error. The NSLog should be %d, it is an integer. NSLog(@"Found %d character(s).", characters.count); //
vodkhang
The correct format specifier would actually be `%lu`, for an `unsigned long`, since NSArray's `count` method returns an `NSUInteger`.
Peter Hosey
Curyous: The crash is caused by trying to send a `description` message (which is what `%@` does, and prints the result of) to an `NSUInteger`. That's wrong because `NSUInteger`s aren't objects; you can only send a message to an object. You need to use a different specifier, such as `%lu` , to print `NSUInteger` values. For the full list, see: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
Peter Hosey
really? I didn't recognize about that because %d always work for me
vodkhang
vodkhang: `%d` is a specifier for a signed `int`. `NSUInteger` is an `unsigned int` on 32-bit Macs (on which `unsigned long` is the same size), and an `unsigned long` on 64-bit Macs (on which it is twice as big as an `int`) and the iPhone. This means that `%d` will produce wrong results for large enough values; on a 64-bit Mac, the low half of the value may be lopped off (you only told `NSLog` to print the high 32 bits' worth), and on any machine, it may print a large enough value as negative. The correct specifier, which doesn't have these problems, is `%lu`.
Peter Hosey
Thanks, I got it.
vodkhang
Wow, that was a whole lot simpler than I was expecting, thanks. I originally had a much more complicated issue and was just using the comment to ensure that I fixed it.
Curyous
A: 

The cash is caused because count is a method, not an property, of NSArray so you can't use the dot notation to call it. You have to use bracket notation thusly:

 NSLog(@"Found %d character(s).", [characters count]);

It's an easy mistake to make if you've worked in a lot of languages that do use dot notation to call methods.

TechZen