3

I see a few similar questions, but no simple answers. I'm just playing around with NSMutableArray's to get a feel for them before I actually use them in my real project. For some reason, it's giving me an EXC_BAD_ACCESS error when I try to call count on the array, and I can't figure out why.

    - (void) applicationDidFinishLaunching:(UIApplication*)application 
{   
    // Create window and make key
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window makeKeyAndVisible];

    NSMutableArray* test = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"first!"], [NSString stringWithFormat:@"second!"], nil];
    [test insertObject:[NSString stringWithFormat:@"inserted"] atIndex:0];
    NSLog(@"%@", [test objectAtIndex:0]);
    NSLog(@"%@", [test objectAtIndex:1]);
    NSLog(@"%@", [test objectAtIndex:2]);
    NSLog(@"%@", [test count]); //bad access here
}

All the inserting and accessing EXCEPT the count method work just fine. I don't see why this isn't working, and would greatly appreciate some help. Thanks!

PengOne
  • 48,188
  • 17
  • 130
  • 149
TNTisCOOL
  • 125
  • 1
  • 9

4 Answers4

8

The %@ format specifier prints objects. The return value of -count is just an unsigned integer. You should use the format specifier %u for that type.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • Hmm, ok. Thanks a lot! Seems like a weird error to get for just having the wrong format specifier, haha. – TNTisCOOL Jun 18 '11 at 20:52
  • 4
    @TNTisCOOL: No, it's not weird once you know why it crashes. The `%@` specifier wants the *address* of an object and wants to call `[theObject description]` on it. When you use `[test count]` you actually pass a number like 3 that is then interpreted as an address, but `3` would be an invalid address and trying to access it (dereference it) causes the crash. – DarkDust Jun 18 '11 at 20:57
  • Oh, that makes sense! Thanks! I wish I had the reputation to give you an up vote. – TNTisCOOL Jun 18 '11 at 21:09
2

The problem is that [test count] returns an NSUInteger not a pointer (to an NSObject). Try this instead:

NSLog(@"%u", [test count]);

Note that using %d also works, but %u is preferred.

PengOne
  • 48,188
  • 17
  • 130
  • 149
1

- (NSUInteger)count; returns an NSUInteger.

Use this instead:

NSLog(@"%u", [test count]); //bad access here
Dan Sandland
  • 7,095
  • 2
  • 29
  • 29
  • It returns an `NSUInteger`, not an `NSInteger` – Alex Reynolds Jun 18 '11 at 21:12
  • 1
    Aaand.. to clarify, the bad access occurs because the `"%@"` format assumes that the handed argument is an `NSObject`, and tries to call the method `-description`, which should return an `NSString*`, but since `NSUInteger` is just a primitive, it cannot respond to such methods. – Can Apr 17 '14 at 03:04
0

count is working just fine. It does however return a NSUInteger primitive and not a pointer to a NSObject subclass. The %@ string formatter expects a point to an object and logs the NSString returned from that object's -description method. When you pass it a NSUInteger NSLog assumes it to be an object pointer and dutifully tries to send a -description message to memory address 3 which causes that EXEC_BAD_ACCESS.

Jonah
  • 17,918
  • 1
  • 43
  • 70