2011-09-03 80 views
0

我有一个对象我想变成一个实例变量。这工作:为什么我无法将此对象转换为实例变量?

ZipFile *newZipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate]; 

但是当我尝试将其更改为这它不起作用:

.H:

@interface PanelController : NSWindowController <NSWindowDelegate> { 
    ZipFile *_zipFile; 
} 
@property (nonatomic, assign) ZipFile *zipFile; 

.M:

@synthesize zipFile = _zipFile; 
... 
// get a syntax error here 
zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate]; 

编辑:我能够解决这个问题,把它放在我的界面中,并删除@property:

ZipFile *newZipFile; 

我想我不能分配getter和setter方法只是任何对象?但为什么不会它工作,如果我做的:

ZipFile *zipFile; 

回答

5

没有名为zipFile伊娃。您的意思是:

_zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate]; 

或:

self.zipFile = [[[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate] autorelease]; 

注:你可能希望你的属性为retainassign适用于您不拥有的房产(如代表)。 assign属性是不安全的,因为可以很容易地变成悬挂指针。

3
@synthesize zipFile = _zipFile; 
... 
// get a syntax error here 
zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate]; 

@synthesize说,你的财产被命名为zipFile,但是变量的支持是_zipFile

你没有zipFile变量,所以分配行是错误的。

_zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate]; 

是正确的。

相关问题