Skip to content

Instantly share code, notes, and snippets.

@bofh
Created October 28, 2011 19:11
Show Gist options
  • Save bofh/1323181 to your computer and use it in GitHub Desktop.
Save bofh/1323181 to your computer and use it in GitHub Desktop.
Exploring @defs replacement in ObjC 2.0
/*
* Compile with gcc -framework Foundation
*/
#import <Foundation/Foundation.h>
struct internals {
int a;
int b;
};
@interface Test : NSObject {
struct internals internals_;
}
- (void *)internals;
- (void)printInternals;
@end
@implementation Test
- (void *)internals
{
return &internals_;
}
- (void)printInternals
{
NSLog(@"a = %d\tb = %d", internals_.a, internals_.b);
}
@end
int main(void)
{
Test *test = [Test new];
struct internals *a;
a = [test internals];
a->a = 1;
a->b = 2;
[test printInternals];
NSLog(@"%p %p (%d)", test, a, (void *)a - (void *)test);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment