Created
April 4, 2014 21:08
-
-
Save SpacyRicochet/9983194 to your computer and use it in GitHub Desktop.
Accessibility of ivars synthesised from readonly @properties in subclasses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface Animal : NSObject | |
{ | |
// The instance variable 'name' that would normally be generated by @property, is not visible in subclasses. | |
// Thus, leaving the subclasses with no way to set the 'name'. | |
// Because of this, we have to declare the instance variable publicly as well. | |
NSString *_name; | |
} | |
@property (nonatomic, readonly) NSString *name; | |
@end | |
@implementation Animal | |
@end | |
/////// | |
@interface Wolf : Animal | |
- (instancetype)initWithName:(NSString *)name; | |
@end | |
@implementation Wolf | |
- (instancetype)initWithName:(NSString *)name | |
{ | |
self = [super init]; | |
if (self) { | |
_name = name; | |
} | |
return self; | |
} | |
@end | |
int main(int argc, char * argv[]) | |
{ | |
@autoreleasepool { | |
Wolf *wolf = [[Wolf alloc] initWithName:@"Okami"]; | |
NSLog(@"%@", wolf.name); // Outputs 'Okami'. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment