Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access modifier visibility in objective c

There are 3 modifiers: @private, @protected (default) and @public. So if i define a instance variable as private then that should not be accessible from anywhere. For E.g. -

@interface A {
    @private
    NSString *a;
}
@property(nonatomic, retain) NSString *a;

Now inside implementation of some other interface/class B-

-(void)getSomeValue {
     A *object = [[A alloc] init];
     NSString *value = object.a;
     .........
 }

Here i am able to access instance variable, although i defined that as private.

It is a bit confusing, although when i look into details of this statement, then it is clear that it is calling the getter of a, but then also it seems confusing and it is against the concept of OOPS.

Anyone having any thought on this?

like image 881
rishi Avatar asked Dec 04 '22 18:12

rishi


1 Answers

It's not the instance variable you're accessing but the property you declared. Don't declare the property if you do not want the instance variable to be visible outside the class.

like image 132
tobiasbayer Avatar answered Feb 15 '23 21:02

tobiasbayer