Effiective objecttive -c2.0 This book says that the initialization method and dealloc method should always read and write data through instance variables. After reading it for a long time, I didn’t understand the reason? Has anyone read this book?
黄舟2017-05-17 10:07:31
Isn’t it very clear in the book:
_name = @"Jack"
It is fast to directly assign values to variables without sending messages through setters.
For the following name
attributes:
@property (nonatomic, copy) NSString *name;
Direct assignment is: _name = @"Jack";
,通过 self.name = @"Jack"
其实等同于 _name = @"Jack".copy
;
self.name = @"Jack"
会触发KVO,_name = @"Jack"
will trigger KVO,
self.name = @"Jack"
NSString *str = _name
,赋值用 self.name = @"Jack"
So there is a reasonable compromise solution, which is to use NSString *str = _name
when reading data, and use
self.name = @"Jack"
可能不等同于 _name = @"Jack".copy
Another thing to note is that subclasses may override setter methods, and using
_name = @"Jack".copy
. I don’t understand what you are unclear about, so I can only briefly describe it using my ideas. 🎜