3 回答
TA贡献1856条经验 获得超11个赞
@synthesize
@interface Foo : Bar { Baz *_qux;}@property (retain) Baz *qux;@end@implementation Foo@synthesize qux = _qux;- (void)dealloc { [_qux release]; [super dealloc];}@end
_qux
self.qux
[self qux]
qux
self
.
-dealloc
- (void)dealloc { self.qux = nil; // [self setQux:nil]; [super dealloc];}
qux
您可能最终会触发一些意外的通知。其他对象可能正在观察对 qux
,在使用访问器方法更改访问器方法时记录。 (并不是每个人都同意这一点:)像访问器那样将指针归零可能会隐藏程序中的逻辑错误。如果您曾经访问对象的实例变量 后
对象已被解除分配,您正在做一些严重错误的事情。因为目标-C nil
-消息传递语义,但是,您永远不会知道,已经使用访问器设置为 nil
..如果直接释放实例变量,而不是将引用归零,那么访问已释放的对象就会引起响亮的声音。 EXC_BAD_ACCESS
.
@interface Foo : Bar@property (retain) Baz *qux;@end@implementation Foo@synthesize qux = _qux;- (void)dealloc { [_qux release]; [super dealloc];}@end
Foo
_qux
-qux
-setQux:
.
@interface Foo : Bar@property (retain) Baz *qux;@end@implementation Foo@synthesize qux;- (void)dealloc { [qux release]; [super dealloc];}@end
qux
self->qux
self.qux
([self qux]
self.qux = blah;
([self setQux:blah]
).
TA贡献1911条经验 获得超7个赞
self.title = title
self.rating = rating
:
@implementation ScaryBugData@synthesize title;@synthesize rating;- (id)initWithTitle:(NSString *)title rating:(float)rating { if (self = [super init]) { self.title = title; // Warning. Local declaration hides instance variable self.rating = rating; // Warning. Local declaration hides instance variable } return self;}@end
@implementation ScaryBugData @synthesize title = _title; @synthesize rating = _rating; - (id)initWithTitle:(NSString *)title rating:(float)rating { if (self = [super init]) { self.title = title; // No warning self.rating = rating; // No warning } return self; } @end
TA贡献1876条经验 获得超6个赞
在DIDFinishLaunchingWithOptions:Method应用程序中,使用Self引用窗口和viewControllerIvars
window
viewController
- 3 回答
- 0 关注
- 307 浏览
添加回答
举报