Headers and generated instance variables

When a class declares a property in its header, only the accessors for this property are visible to other objects. Non-BNREmployee objects (including subclasses) cannot directly access the instance variables generated by property declarations.

For example, imagine that BNRPerson.h declares this property:

 ​ ​@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​f​r​i​e​n​d​s​;​

In BNREmployee.m, even though BNREmployee is a subclass of BNRPerson, you cannot access the _friends instance variable:

 ​ ​[​_​f​r​i​e​n​d​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​S​u​s​a​n​"​]​;​ ​/​/​ ​E​r​r​o​r​!​

However, you can use the accessor:

 ​ ​ ​ ​[​s​e​l​f​.​f​r​i​e​n​d​s​ ​a​d​d​O​b​j​e​c​t​:​@​"​S​u​s​a​n​"​]​;​

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset