我有一个对象我想变成一个实例变量。这有效:
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;
我想我不能将setter和getter分配给任何对象?但是,如果我这样做,为什么它不会起作用:
ZipFile *zipFile;
答案 0 :(得分:5)
没有名为zipFile
的ivar。你的意思是:
_zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate];
或:
self.zipFile = [[[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate] autorelease];
注意:您可能希望自己的财产为retain
。 assign
适用于您不拥有的属性(如委托)。 assign
属性不安全,因为很容易成为悬空指针。
答案 1 :(得分: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];
是对的。