3 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
@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
您可能最終會(huì)觸發(fā)一些意外的通知。其他對(duì)象可能正在觀察對(duì) qux
,在使用訪問器方法更改訪問器方法時(shí)記錄。 (并不是每個(gè)人都同意這一點(diǎn):)像訪問器那樣將指針歸零可能會(huì)隱藏程序中的邏輯錯(cuò)誤。如果您曾經(jīng)訪問對(duì)象的實(shí)例變量 后
對(duì)象已被解除分配,您正在做一些嚴(yán)重錯(cuò)誤的事情。因?yàn)槟繕?biāo)-C nil
-消息傳遞語義,但是,您永遠(yuǎn)不會(huì)知道,已經(jīng)使用訪問器設(shè)置為 nil
..如果直接釋放實(shí)例變量,而不是將引用歸零,那么訪問已釋放的對(duì)象就會(huì)引起響亮的聲音。 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貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊
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貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
在DIDFinishLaunchingWithOptions:Method應(yīng)用程序中,使用Self引用窗口和viewControllerIvars
window
viewController
- 3 回答
- 0 關(guān)注
- 340 瀏覽
添加回答
舉報(bào)