About chapter 22. class Extensions

@interface BNREmployee ()
@property (nonatomic) unsigned int officeAlarmCode;

@implementation BNREmployee

BNREmployee *mikey = [[BNREmployee alloc] init];
unsigned int mikeysCode = mikey.officeAlarmCode;

you said that ‘mikey’ is a non-BNREmployee object.
but i think ‘mikey’ is ‘BNREmployee object’?
i don’t know why you say that mikey is ‘non-BNREmployee object’?
so i think ‘mikey’ is employee object variable, so it can access its alarm code of private property.
but it can’t.
teach me why it can’t access its alarm code. plz.

@interface BNREmployee ()
@property (nonatomic) unsigned int officeAlarmCode;

@implementation BNREmployee[/code][/quote]
A BNREmployee object can access the officeAlarmCode property in the implementation file.

However, since the property is declared in the implementation file as a private property, if you try to access it from outside of the implementation file, for example in main.m, you will get an error at compilation time.

thanks for helping.
when i have question to you, and then you answered to me.
it’s very understanding more than studying alone.

I’m still a little bit puzzled by this same part in the chapter by the way.
The way the book reads, it’s saying that an object that is an instance of the class BNREmployee can in fact still access the officeAlarmCode method. In this particular example mikey is literally a pointer to an instance of BNREmployee and, how I understand it, should have access to this particular method.

On the other hand, if I assume that anything that tries to access the method officeAlarmCode outside of the implementation file, wether it be an instance of BNREmployee or not will fail, it makes more sense.

Or is the fact that mikey is a pointer and not an actual instance of the object the fact it’s not working? If so, is there a way to access this method in main.m after all?
Little bit confused by this little snippet.

Edit: I just realised how this actually works and think the book leaves out an important bit of information for this part in the chapter to really make sense. What effectively happens by moving the ivar officeAlarmCode to a class extension is that it becomes impossible to set or get the ivar. I believe what needs to be added additionally after adding it to the class extension, are new methods to set, get and/or remove values to the ivar.

The way I read this part:

BNREmployee *mikey = [[BNREmployee alloc] init];
unsigned int mikeysCode = mikey.officeAlarmCode;[/quote]

Is that the unsigned int called mikeysCode (which is NOT an instance of BNREmployee) can NOT then ask mikey (who IS an instance of BNREmployee) for his officeAlarmCode.


unsigned int mikeyCode = (remember that = is not equals but set this variable to the next thing)

is not able to access


Does that help explain it better fly2web and Arthurzwit?

To set officeAlarmCode for employee object, you need to explicitly declare a setter in the .h file and add it’s implementation in the .m file.

#import “BNRPerson.h”
#import “BNRAsset.h”

@interface BNREmployee : BNRPerson

NSMutableArray *_assets;

@property (nonatomic) int employeeID;
//@property (nonatomic) int officeAlarmCode;
@property (nonatomic) NSDate *hireDate;

@property (nonatomic, copy) NSArray *assets;

  • (double) yearsOfEmployment;

  • (void) addAsset:(BNRAsset *)a;

  • (void) removeAsset:(BNRAsset *)a;

  • (void) removAllAssets;

  • (int) valueOfAssets;
    [color=#FF0040]- (void) setOfficeAlarmCode: (int ) code;

- (void) setOfficeAlarmCode:(int )code {
_officeAlarmCode = code;

Why would you need to declare a setter if the officeAlarmCode is declared as a @property in BNREmployee.m as the book says? I thought that a @property was suppose to synthesize a setter and a getter.