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​"​]​;​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.