I have the following code:
@interface ClassA : NSObject
@property (nonatomic, retain, readonly) id my_property;
@end
@implementation ClassA
@synthesize my_property;
- (id)init {
if (self = [super init]) {
self->my_property = [NSNumber numberWithInt:1];
}
return self;
}
- (void)debug {
NSLog(@"%@", self->my_property);
}
@end
#import "ClassA.h"
@interface ClassB : ClassA
@end
#import "ClassB.h"
@implementation ClassB
@synthesize my_property;
- (id)init {
if (self = [super init]) {
self->my_property = [NSNumber numberWithInt:2];
}
return self;
}
@end
I call the above code like this:
ClassB *b = [[ClassB alloc] init];
[b debug];
Output signal 1. If I use the method -[Class A debug]to use self.my_property, the output will be 2.
My (limited) understanding is that with the “modern” Objective-C development environment, ivars class generates dynamically. Can subclasses of classes with these dynamically generated ivars access the specified instance variables? If I do not include the line @synthesize my_propertyin ClassB.m, the compiler will give me an error:
error: 'struct ClassB' does not have a name named 'my_property'
, -[ClassB init], , ivar, , - , . , API?
. , . , @interface , . SO:
, , , , , .