Skip to content

Instantly share code, notes, and snippets.

@jianzwang
Created April 10, 2013 07:07
Show Gist options
  • Select an option

  • Save jianzwang/5352428 to your computer and use it in GitHub Desktop.

Select an option

Save jianzwang/5352428 to your computer and use it in GitHub Desktop.
objc_getAssociatedObject & objc_setAssociatedObject example
static char overviewKey;
NSArray *array =
[[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];
// For the purposes of illustration, use initWithFormat: to ensure
// the string can be deallocated
NSString *overview =
[[NSString alloc] initWithFormat:@"%@", @"First three numbers"];
objc_setAssociatedObject (
array,
&overviewKey,
overview,
OBJC_ASSOCIATION_RETAIN
);
NSString *payload = objc_getAssociatedObject(array, &overviewKey);
NSLog(@"%@",payload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment