Created
April 10, 2013 07:07
-
-
Save jianzwang/5352428 to your computer and use it in GitHub Desktop.
objc_getAssociatedObject & objc_setAssociatedObject example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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