为什么我的数组实例超出了范围?

ice*_*ace 0 iphone cocoa objective-c

有人可以告诉我为什么我的阵列超出范围?这是我的班级:

// 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;
...
Run Code Online (Sandbox Code Playgroud)

有人可以指出这里有什么问题吗?非常感谢!

Pet*_*sey 5

事实并非如此.

对象不能超出范围,因为对象没有范围.它们可以是无法访问的,当你没有任何变量持有对象的指针时会发生这种情况.

变量可能超出范围.您只能在声明它的同一范围内使用变量; 你不能开始复合语句,声明变量,完成复合语句,并使用变量,你不能在一个函数或方法中声明一个变量,然后在另一个函数或方法中使用它.

你在另一个问题中说,调试器告诉你变量超出了范围.这意味着其中之一 三件事:

  1. 变量确实超出了范围.移动变量或移动使用它的代码,或者只是提前中断调试器(如果需要,使用断点).
  2. 调试器只是愚蠢的.这种情况发生了很多.尝试使用该po命令或将代码与代码一起使用NSLog.
  3. 您正在尝试检查属性访问表达式.根据定义,属性访问表达式必须发送一个可能有副作用的访问者消息; 出于这个原因,调试器不会仅仅因为你将鼠标悬停在表达式上而这样做,因为这很容易被偶然发生.您必须使用po调试器控制台中的命令发送访问者消息并打印结果的描述.