I have a object I want to turn into an instance variable. This works:
ZipFile *newZipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate];
But when I try to change it to this it doesn't work:
.h:
@interface PanelController : NSWindowController <NSWindowDelegate> {
ZipFile *_zipFile;
}
@开发者_C百科property (nonatomic, assign) ZipFile *zipFile;
.m:
@synthesize zipFile = _zipFile;
...
// get a syntax error here
zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate];
EDIT: I was able to fix this by putting this in my interface and getting rid of the @property:
ZipFile *newZipFile;
I guess I can't assign setters and getters to just any object? But why won't it work if I do:
ZipFile *zipFile;
There is no ivar named zipFile
. Did you mean:
_zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate];
or:
self.zipFile = [[[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate] autorelease];
Note: You probably want your property to be retain
. assign
is for properties you do not own (like delegates). assign
properties are unsafe because can easily become dangling pointers.
@synthesize zipFile = _zipFile;
...
// get a syntax error here
zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate];
Your @synthesize
says that your property is named zipFile
, but the variable backing it is _zipFile
.
You have no zipFile
variable, so the assignment line is wrong.
_zipFile = [[ZipFile alloc] initWithFileName:zipPath mode:ZipFileModeCreate];
is correct.
精彩评论