arrayOfBookViews = [[NSMutableArray alloc] init];
BookView *book1 = [[BookView alloc] init];
[arrayOfBookViews addObject:book1];
BookView *book2 = [[BookView alloc] init];
[arrayOfBookViews addObject:book2];
NSLog(@"%@",arrayOfBookViews);
NSLog(@"%@",arrayOfBookViews.count);
Running this code gives me:
(
“”,
“”
)
which is due to the second last line. The last line then throws me a exc_bad_access memory error. Since the array as well as its objects are properly allocated and initialized, I don’t see why asking for the count of the array should give me a memory problem. I’m currently using automatic reference counting in this program with xcode 4.
Please explain why the last line in the code produces a memory error. Thanks.
arrayOfBookViews.countreturns anNSUInteger.NSUIntegeris not an object, its a primitive. The%@format specifier just callsdescriptionon the object passed to it, so you tried to call a method on a primitive which is invalid.Change the log to
NSLog(@"%d",arrayOfBookViews.count);and you’ll get your result you wanted.