Reputation: 951
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!
Upvotes: 0
Views: 494
Reputation: 96333
It isn't.
An object cannot be out of scope, because objects do not have scope. What they can be is unreachable, which is what happens when you don't have any variables holding the object's pointer.
Variables can be out of scope. You can only use a variable within the same scope in which you declared it; you can't begin a compound statement, declare a variable, finish the compound statement, and use the variable, and you can't declare a variable in one function or method and then use it in a different one.
You said in your other question that it's the debugger telling you the variable is out of scope. This means one of two three things:
po
command or sprinkle your code with NSLog
statements instead.po
command in the Debugger Console to send the accessor message and print the description of the result.Upvotes: 5