Skip to content

Instantly share code, notes, and snippets.

@d-ronnqvist
Created July 14, 2014 09:13
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 d-ronnqvist/68d5fbed8f14aef10333 to your computer and use it in GitHub Desktop.
Save d-ronnqvist/68d5fbed8f14aef10333 to your computer and use it in GitHub Desktop.
Synthesizing a read-only property in a subclass overrides custom accessor
@interface Foo : NSObject
// some read-only property ...
@property (nonatomic, copy, readonly) NSString *bar;
@end
@implementation Foo
// ... with a custom accessor
- (NSString *)bar
{
return @"Foo";
}
@end
// and a subclass ...
@interface FooSubclass : Foo
@end
@implementation FooSubclass
// ... that synthesizes that property
@synthesize bar = _bar;
@end
// If you create an instance of the subclass and access the `bar` property it will not use the custom accessor
FooSubclass *foo = [FooSubclass new];
NSLog(@"foo.bar = %@", [foo bar]); // will not call the accessor in Foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment