I have a doubt regarding property redeclaration
Overview:
- class "A" is the parent class with a readonly property int n1;
- class "B" is the subclass, which redeclares the property as read write
- using the setter of class "B" the property value is set as 20
- when i print the value using the getter and the instance variable I seem to get different values
Points to note: - Memory management = ARC (Automatic Reference Counting)
Question:
- When I print the values of self.n1 and _n1 why do I get different values ?
- My expected behavior and actual behavior don't match why (Pls scroll down to see the actual vs expected) ?
Code: (in separate files)
A.h
#import<Foundation/Foundation.h>
@interface A : NSObject
@property (readonly) int n1;
- (void) display;
@end
A.m
#import "A.h"
@implementation A
@synthesize n1 = _n1;
- (void) display
{
printf("_n1 = %i\n", _n1); //I expected _n1 and self.n1 to display the same value
printf("self.n1 = %i\n\n", self.n1); //but they seem to display different values
}
@end
B.h
#import"A.h"
@interface B : A
@property (readwrite) int n1;
@end
B.m
#import"B.h"
@implementation B
@synthesize n1 = _n1;
@end
test.m
#import"B.h"
int main()
{
system("clear");
B* b1 = [[B alloc] init];
b1.n1 = 20;
[b1 display]; //Doubt - my expected behavior is different from actual behavior
return(0);
}
Expected Behavior:
_n1 = 20
self.n1 = 20
Actual Behavior:
_n1 = 0
self.n1 = 20