我一直想知道属性的东西.使用属性时,是否需要覆盖发布消息以确保属性是已发布的属性?
即以下(虚构)示例是否足够?
@interface MyList : NSObject {
NSString* operation;
NSString* link;
}
@property (retain) NSString* operation;
@property (retain) NSString* link;
@end
@implementation MyList
@synthesize operation,link;
@end
Run Code Online (Sandbox Code Playgroud)
Phi*_*ert 13
您应该始终在dealloc中释放支持变量:
- (void) dealloc {
[operation release];
[link release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
其他方式:
- (void) dealloc {
self.operation = nil;
self.link = nil;
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
这不是释放对象的首选方式,但如果您使用的是合成后备变量,那么这是唯一的方法.
注意:为了弄清楚为什么这样做,让我们看一下setint for link属性的综合实现,以及当它设置为nil时会发生什么:
- (void) setLink:(MyClass *) value {
[value retain]; // calls [nil retain], which does nothing
[link release]; // releases the backing variable (ivar)
link = value; // sets the backing variable (ivar) to nil
}
Run Code Online (Sandbox Code Playgroud)
因此净效应是它将释放伊娃.
| 归档时间: |
|
| 查看次数: |
3335 次 |
| 最近记录: |