Created
July 14, 2014 09:13
-
-
Save d-ronnqvist/68d5fbed8f14aef10333 to your computer and use it in GitHub Desktop.
Synthesizing a read-only property in a subclass overrides custom accessor
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
@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