3 回答
TA貢獻1810條經驗 獲得超5個贊
@synthesize
@interface Foo : Bar {
Baz *_qux;}@property (retain) Baz *qux;@end@implementation Foo@synthesize qux = _qux;- (void)dealloc {
[_qux release];
[super dealloc];}@end_quxself.qux[self qux]quxself.
-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];}@endFoo_qux-qux-setQux:.
@interface Foo : Bar@property (retain) Baz *qux;@end@implementation Foo@synthesize qux;- (void)dealloc {
[qux release];
[super dealloc];}@endquxself->quxself.qux ([self qux]self.qux = blah; ([self setQux:blah]).
TA貢獻1827條經驗 獲得超8個贊
self.title = titleself.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;
}
@endTA貢獻1829條經驗 獲得超4個贊
在DIDFinishLaunchingWithOptions:Method應用程序中,使用Self引用窗口和viewControllerIvars
windowviewController
- 3 回答
- 0 關注
- 347 瀏覽
添加回答
舉報
