3 回答

TA貢獻1865條經驗 獲得超7個贊
@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貢獻1818條經驗 獲得超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貢獻1848條經驗 獲得超6個贊
在DIDFinishLaunchingWithOptions:Method應用程序中,使用Self引用窗口和viewControllerIvars
window
viewController
- 3 回答
- 0 關注
- 468 瀏覽
添加回答
舉報