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)
有人可以指出这里有什么问题吗?非常感谢!
事实并非如此.
对象不能超出范围,因为对象没有范围.它们可以是无法访问的,当你没有任何变量持有对象的指针时会发生这种情况.
变量可能超出范围.您只能在声明它的同一范围内使用变量; 你不能开始复合语句,声明变量,完成复合语句,并使用变量,你不能在一个函数或方法中声明一个变量,然后在另一个函数或方法中使用它.
你在另一个问题中说,调试器告诉你变量超出了范围.这意味着其中之一二 三件事:
po命令或将代码与代码一起使用NSLog.po调试器控制台中的命令发送访问者消息并打印结果的描述.| 归档时间: |
|
| 查看次数: |
490 次 |
| 最近记录: |