I have an NSMutableArray defined as a property, synthesized and I have assigned a newly created instance of an NSMutableArray. But after this my application always crashes whenever I try adding an object to the NSMutableArray.
Page.h
@interface Page : NSObject
{
NSString *name;
UIImage *image;
NSMutableArray *questions;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, copy) NSMutableArray *questions;
@end
Page.m
@implementation Page
@synthesize name, image, questions;
@end
Relevant code
Page *testPage = [[Page alloc] init];
testPage.image = [UIImage imageNamed:@"Cooperatief leren Veenman-11.jpg"];
testPage.name = [NSString stringWithString:@"Cooperatief leren Veenman-11.jpg"];
testPage.questions = [[NSMutableArray alloc] init];
[testPage.questions addObject:[NSNumber numberWithFloat:arc4random()]];
The debugger reveals that the moment I use testPage.questions = [[NSMutableArray alloc] init]; the type of testPage.questions changes from NSMutableArray* to __NSArrayL* (or __NSArrayI*, not sure). I suspect this to be the problem, but I find it extremely odd. Anyone know what’s happening here?
The problem is that you’ve declared the property as
copy. This means your setter is going to be implemented something like this:The kicker here is that if you
-copyan array (whether immutable or mutable), you will always get an immutableNSArray.So to fix this, change the property to be
retaininstead ofcopy, and also fix this memory leak:It should be: