Accessing to class extension with that class's object


#1

Hello there,

Actually i’m starting objective c from scratch with this book. i reached 22 and came to example.
when i move OfficeAlarmCode to class extension, book says that it has a two effects and first is:

But “mikey” is a BNREmployee object, so it can access officeAlarmCode. compiler returns error. why? :question:


#2

The same question! Can anybody solve this issue?


#3

[quote=“miximixi”]Hello there,

Actually i’m starting objective c from scratch with this book. i reached 22 and came to example.
when i move OfficeAlarmCode to class extension, book says that it has a two effects and first is:

But “mikey” is a BNREmployee object, so it can access officeAlarmCode. compiler returns error. why? :question:[/quote]
That (underlined) statement is very confusing; it implies that objects that are instances of BNREmployee can access that property from anywhere:

... BNREmployee *mikey = [[BNREmployee alloc] init]; unsigned int mikeysCode = mikey.officeAlarmCode; ...
because mikey is an instance of BNREmployee and should be able to see its own office alarm code.

The statement should have expressed where the property can be seen, instead of the object instances seeing the property.

There won’t be any confusion, if we rewrite that statement as “This property can no longer be seen from outside the methods of BNREmployee class.

Access from outside BNREmployee methods is not possible:

int main ()
{
   ...
   BNREmployee *mikey = [[BNREmployee alloc] init];
   unsigned int mikeysCode = mikey.officeAlarmCode;
   ...
}

BNREmployee methods can access it:

// BNREmployee.m

@interface BNREmployee ()

@property NSUInteger officeAlarmCode;

@end

@implementation BNREmployee
...
- (void)enterNewAlarmCode
{
   self.officeAlarmCode = [self pickUnusedAlarmCode];
   [self rememberAlarmCode:self.officeAlarmCode];
}
...
@end

Trying to explain things using OO lingo can sometimes produce confusing results. I hope that, in trying to clear it up, I haven’t caused more confusion.


#4

Dear ibex,

Thanks for your comprehensive answer. now it cleared for me. :smiley:

[quote=“ibex10”][quote=“miximixi”]Hello there,

Actually i’m starting objective c from scratch with this book. i reached 22 and came to example.
when i move OfficeAlarmCode to class extension, book says that it has a two effects and first is:

But “mikey” is a BNREmployee object, so it can access officeAlarmCode. compiler returns error. why? :question:[/quote]
That (underlined) statement is very confusing; it implies that objects that are instances of BNREmployee can access that property from anywhere:

... BNREmployee *mikey = [[BNREmployee alloc] init]; unsigned int mikeysCode = mikey.officeAlarmCode; ...
because mikey is an instance of BNREmployee and should be able to see its own office alarm code.

The statement should have expressed where the property can be seen, instead of the object instances seeing the property.

There won’t be any confusion, if we rewrite that statement as “This property can no longer be seen from outside the methods of BNREmployee class.

Access from outside BNREmployee methods is not possible:

int main ()
{
   ...
   BNREmployee *mikey = [[BNREmployee alloc] init];
   unsigned int mikeysCode = mikey.officeAlarmCode;
   ...
}

BNREmployee methods can access it:

// BNREmployee.m

@interface BNREmployee ()

@property NSUInteger officeAlarmCode;

@end

@implementation BNREmployee
...
- (void)enterNewAlarmCode
{
   self.officeAlarmCode = [self pickUnusedAlarmCode];
   [self rememberAlarmCode:self.officeAlarmCode];
}
...
@end

Trying to explain things using OO lingo can sometimes produce confusing results. I hope that, in trying to clear it up, I haven’t caused more confusion.[/quote]