Skip to content

Instantly share code, notes, and snippets.

@SpacyRicochet
Created April 4, 2014 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpacyRicochet/9983194 to your computer and use it in GitHub Desktop.
Save SpacyRicochet/9983194 to your computer and use it in GitHub Desktop.
Accessibility of ivars synthesised from readonly @properties in subclasses
#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