我有一个对象,我想把它变成一个实例变量。这是可行的:
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];
编辑:我能够通过将这个放入我的界面并去掉@属性来修复这个问题:
ZipFile *newZipFile;
我想我不能将setter和getter赋值给任何对象?但是,如果我这样做了,为什么它不起作用:
ZipFile *zipFile;
发布于 2011-09-03 21:57:49
没有叫zipFile
的依瓦尔。你的意思是:
_zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate];
或者:
self.zipFile = [[[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate] autorelease];
注意:你可能希望你的属性是retain
。assign
用于您不拥有的属性(如委托)。assign
属性是不安全的,因为它很容易变成悬空指针。
发布于 2011-09-03 21:59:19
@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];
是正确的。
https://stackoverflow.com/questions/7293482
复制相似问题