Reputation: 3158
In one class, I define an NSMutableArray
with getters and setters:
@interface ArrayClass : NSObject {
NSMutableArray *array;
}
@property (nonatomic, strong) NSMutableArray *array;
@end
Then within the implementation file, I alloc
init
the mutable array:
#import "ImageUploader.h"
@implementation ArrayClass
@synthesize array;
- (id)init {
self = [super init];
if (self != nil) {
NSLog(@"ArrayClass inited");
array = [[NSMutableArray alloc] init];
}
return self;
}
@end
Then I initialize an instance of this class from another class:
ArrayClass *arrayClass = [[ArrayClass alloc] init];
[arrayClass.array addObject:image];
NSUInteger count = [arrayClass.array count];
NSLog(@"%@", count);
But when I try to add an object to the mutable array, the app crashes and Xcode 4.3 shows:
Removing the addObject
call makes the app run fine. What am I doing wrong that would cause the app to crash?
Upvotes: 0
Views: 619
Reputation: 3842
You are using an %@
format specifier, which is for Cocoa objects only, for an NSUInteger
, which is a typedef on an ordinary unsigned int
. Use %d, %i or %u instead.
Upvotes: 1
Reputation: 62686
Agree that the logging of count is wrong, but I think the other answers miss a move obvious point: the crash happens on the addObject. This implies that image is nil. NSLog that before the add.
Upvotes: 0
Reputation: 8808
It looks to me like it's crashing when trying to print description, which makes sense because you're using %@
where an integer is expected in your NSLog()
.
Separately, using a mutable property is almost always a bad idea. If it's really a property, you probably want to use an immutable array, and set the whole array when you want to change it.
Upvotes: 0
Reputation: 27984
This is wrong:
NSUInteger count = [arrayClass.array count];
NSLog(@"%@", count);
You want:
NSLog(@"%u", count);
%@
is used to specify that the argument is an object. However, an NSUInteger is a primitive value, not an object. You use %u
for unsigned ints.
Upvotes: 2
Reputation: 112857
NSLog(@"%@", count);
is wrong, use:
NSLog(@"%i", count);
%@
in the format statement expects and must be an object, NSUInteger
is in int, not an object.
Upvotes: 1
Reputation: 6176
try:
NSLog(@"%i", count);
NSUInteger return an INT not an object address
Upvotes: 1