Skip to content

Instantly share code, notes, and snippets.

@n-b
Last active October 29, 2015 13:10
Show Gist options
  • Save n-b/e5dcff9e9c966b414723 to your computer and use it in GitHub Desktop.
Save n-b/e5dcff9e9c966b414723 to your computer and use it in GitHub Desktop.
@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"
}
@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