Since recent times in iOS, we can define the properties that accessors will generate for instance variables. From what I understand, it is not necessary to declare an instance variable, as it will be automatically made for us.
For example, if I write:
@interface MyFirstClass @property (readonly, nonatomic) int size; @end
and in .m
@implementation MyFirstClass @synthesize size; @end
Then, an instance variable called "size" will be added for me, and a method called "- (int) size" will be implemented.
The problem is that when creating the second class MySecondClass, which is a subclass of MyFirstClass, it seems that I cannot access the size of the instance variable in this subclass:
@interface MySecondClass : MyFirstClass @end @implementation MySecondClass - (id)init { if (self = [super init]) { size = 10; // this yields and error } return self; } @end
Are automatically created instance variables private? Is it possible to set them as protected so that I can access them in subclasses? I know that it is possible to declare an instance variable yourself, but I'm just wondering ...
With a superclass like this, it works: (Is it because it is explicitly declared as protected?)
@interface MyFirstClass { int size; // defined expressly and used as @protected } @property (readonly, nonatomic) int size; @end
Thanks for the help! Nicolas.
source share