Anonymous user
Add a variable to a class instance at runtime: Difference between revisions
Add a variable to a class instance at runtime (view source)
Revision as of 09:05, 25 February 2014
, 10 years ago→{{header|Objective-C}}: modernize
(Updated first D entry) |
m (→{{header|Objective-C}}: modernize) |
||
Line 649:
#import <objc/runtime.h>
static char fooKey;
int main (int argc, const char *argv[]) {
@autoreleasepool {
id e = [[NSObject alloc] init];
// set
objc_setAssociatedObject(e, &fooKey,
// get
NSNumber *associatedObject = objc_getAssociatedObject(e, &fooKey);
NSLog(@"associatedObject: %@", associatedObject);
return 0;
}</lang>
Line 673 ⟶ 672:
int main (int argc, const char *argv[]) {
@autoreleasepool {
id e = [[NSObject alloc] init];
// set
objc_setAssociatedObject(e, @selector(foo),
// get
NSNumber *associatedObject = objc_getAssociatedObject(e, @selector(foo));
NSLog(@"associatedObject: %@", associatedObject);
return 0;
}</lang>
|