objective c - Why does initWithCoder need self.property? -
objective c - Why does initWithCoder need self.property? -
i tend regularly utilize self.property in objective c code when accessing variables understand makes utilize of getter/setter methods (either explicitly coded or automatically generated).
another coder told me it's best utilize _property instead, throughout code. understanding _property utilize in init , dealloc methods.
today found bug in code uses initwithcoder. using _property in initwithcoder method , had alter self.property. when setting nsdata object. here original code:
@property (nonatomic, strong) nsdata *data; - (id)initwithcoder:(nscoder *)decoder { self = [super init]; if (!self) { homecoming nil; } _data = [decoder decodeobjectforkey:@"data"]; homecoming self; } and had alter this, work:
@property (nonatomic, strong) nsdata *data; - (id)initwithcoder:(nscoder *)decoder { self = [super init]; if (!self) { homecoming nil; } self.data = [decoder decodeobjectforkey:@"data"]; homecoming self; } my question - why initwithcoder exception here? if it's accepted it's best utilize _property in init method, why initwithcoder different?
it seems general rule is:
use self.property except in init/dealloc methods, _property should used (except in initwithcoder, self.property should used).
is correct?
i not think true must utilize properties in initwithcoder:. have lot of code (and have seen lot) ivar access used in initwithcoder:, if may help hint.
if not using arc, implementation setting _data have problem in object autorelased. under arc code correct.
so, tend think different causing issue in case. example, if utilize kvo, should utilize properties, otherwise kvo-related notifications not generated. should provide more info led think assignment _data cause of issue, , visible effect of issue in other parts of code.
objective-c getter-setter initwithcoder
Comments
Post a Comment