ios - Why do some books say that the initialization method and dealloc method should always read and write data through instance variables?
我想大声告诉你
我想大声告诉你 2017-05-17 10:05:31
0
1
1016

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?

我想大声告诉你
我想大声告诉你

reply all(1)
黄舟

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,

    will not
  • self.name = @"Jack"

    You can perform breakpoint debugging in the setter method, and you will know every assignment.

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

when assigning values.

self.name = @"Jack"可能不等同于 _name = @"Jack".copyAnother thing to note is that subclasses may override setter methods, and using

may not be equivalent to _name = @"Jack".copy.

I don’t understand what you are unclear about, so I can only briefly describe it using my ideas. 🎜
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template