RosettaCodeData/Task/Add-a-variable-to-a-class-instance-at-runtime/Objective-C/add-a-variable-to-a-class-instance-at-runtime-2.m

19 lines
443 B
Mathematica
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
int main (int argc, const char *argv[]) {
2014-04-02 16:56:35 +00:00
@autoreleasepool {
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
id e = [[NSObject alloc] init];
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
// set
objc_setAssociatedObject(e, @selector(foo), @1, OBJC_ASSOCIATION_RETAIN);
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
// get
NSNumber *associatedObject = objc_getAssociatedObject(e, @selector(foo));
NSLog(@"associatedObject: %@", associatedObject);
2013-04-10 22:43:41 -07:00
2014-04-02 16:56:35 +00:00
}
2013-04-10 22:43:41 -07:00
return 0;
}