@property declaration of officeAlarmCode in class extension


#1

Hi everyone, sorry if this has been explained somewhere else, but I wasn’t able to find an answer that was clear enough.

I understand that declaring officeAlarmCode as such

BNREmployee.m

@inteface BNREmployee ()

@property (nonatomic) unsigned int officeAlarmCode;

@end

causes the setter and getter methods of officeAlarmCode to only be accessible within the implementation file (in this case, BNREmployee.m). However, what if we have a BNREmployee object that we need to set an officeAlarmCode to? By putting the @property declaration in the class extension, we can’t do that from main.m.

Naturally, the next step to accomplish this would be to declare the setter and getter methods myself. But this would require me defining the methods in the interface. So what’s the point of putting the officeAlarmCode in a class extension if when I need to set a value to it, I need to publicly declare the methods anyway? Hopefully I’m not missing something here.


#2

This is a valid question for the suppled example, however, I think it helps to think of the example not in practical terms for the very reasoning you provide. I think (and someone correct me if I am wrong) the point of this example is purely theoretical. Yes, you would have to declare and implement internal methods for ‘setting’ and ‘getting’ the alarm code but within those methods you could theoretically control both how the alarm code is stored or how it is returned (eg. you could theoretically have a getter method that returns only the last 3 digits of the alarm code (or base it on some other external condition). At least, I believe this is the point the example is trying to make with regards to class extensions.