Could some one tell me why my array is out of scope? Here's my class:
// Paper.h
@interface Paper : NSObject {
NSMutableArray* items;
}
@property (retain) NSMutableArray* items;
// Paper.m
#import "Paper.h"
@implementation Paper {
@synthesize items;
}
// ParserUtil.m
@implementation ParserUtil {
+(Paper*) parsePaper:(NSString*)file {
...
Paper* paper = [[[Paper alloc] init] autorelease];
// does the following line is the best practice?
paper.items = [[[MutableArray alloc] init] autorelease];
Item* item = ...; // create item instance
[paper.items addObject:item];
return paper;
}
// call the parser method
...
Paper* paper = [[ParserUtil parsePaper:@"SomeFile"] retain];
// when run to this line, the paper.items is out of scope
// seems all the items in the array are dispear
NSMutableArray* items = paper.items;
...
Could someone point out what is wrong here? Many thanks!
@implementationends with@end, not with curly braces. Note also that rather than[[[NSMutableArray alloc] init] autorelease]you can just as easily use[NSMutableArray array].itemsarray for it; the Paper object should create its ownitemsarray. Moreover, getting itsitemsarray should get a copy, so[paper.items addObject:…]should not actually add anything to thepaper'sitems. HavePaperrespond toaddItemsObject:by sending itselfinsertObjectInItemsAtIndex:, which you should have it respond to by sending itsitemsarray (which it should have created ininit) aninsertObjectAtIndex:message.