search

Home  >  Q&A  >  body text

ios - Why do some books say that the initialization method and dealloc method should always read and write data through instance variables?

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?

我想大声告诉你我想大声告诉你2773 days ago1024

reply all(1)I'll reply

  • 黄舟

    黄舟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,

      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. 🎜

    reply
    0
  • Cancelreply