iphone - NSMutableArray crashes when adding after proper initialization -
i have nsmutablearray defined property, synthesized , have assigned newly created instance of nsmutablearray. after application crashes whenever try adding object 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 moment use testpage.questions = [[nsmutablearray alloc] init]; type of testpage.questions changes nsmutablearray* __nsarrayl* (or __nsarrayi*, not sure). suspect problem, find extremely odd. know what's happening here?
the problem you've declared property copy. means setter going implemented this:
- (void) setquestions:(nsmutablearray *)array { if (array != questions) { [questions release]; questions = [array copy]; } } the kicker here if -copy array (whether immutable or mutable), always immutable nsarray.
so fix this, change property retain instead of copy, , fix memory leak:
testpage.questions = [[nsmutablearray alloc] init]; it should be:
testpage.questions = [nsmutablearray array];
Comments
Post a Comment