Last active
October 29, 2015 13:10
-
-
Save n-b/e5dcff9e9c966b414723 to your computer and use it in GitHub Desktop.
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; | |
@interface A : NSObject | |
@end | |
@interface B : A | |
@end | |
// Private objc ivars are really private: | |
@implementation A | |
{ | |
int _ivar; | |
} | |
- (void) assign_inA { _ivar = 42; } | |
- (int) access_fromA { return _ivar; } | |
@end | |
@implementation B | |
{ | |
char* _ivar; | |
} | |
- (void) assign_inB { _ivar = "forty-two"; } | |
- (char*) access_fromB { return _ivar; } | |
@end | |
int main() { | |
id obj = [B new]; | |
[obj assign_inA]; | |
[obj assign_inB]; | |
printf("%i, %s\n", [obj access_fromA], [obj access_fromB]); | |
// prints "42, forty-two" | |
} |
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; | |
// On the other hand, overriding public ivars is forbidden: | |
@interface A : NSObject | |
{ | |
int _ivar | |
} | |
@end | |
@interface B : A | |
{ | |
char* _ivar; // error: duplicate member '_ivar' | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment