Created
December 3, 2013 16:49
-
-
Save maciekish/7772693 to your computer and use it in GitHub Desktop.
Ever dreamed of adding "userInfo" to an UIAlertView? Now you can! This category allows you to assign any object to any object from iOS 3.1 and Mac OS 10.6 and up.
This file contains 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
// | |
// NSObject+Association.h | |
// | |
// Created by Maciej Swic on 03/12/13. | |
// Released under the MIT license. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSObject (Association) | |
- (id)associatedObjectForKey:(NSString*)key; | |
- (void)setAssociatedObject:(id)object forKey:(NSString*)key; | |
@end |
This file contains 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
// | |
// NSObject+Association.m | |
// | |
// Created by Maciej Swic on 03/12/13. | |
// Released under the MIT license. | |
// | |
#import <objc/runtime.h> | |
#import "NSObject+Association.h" | |
@implementation NSObject (Association) | |
static char associatedObjectsKey; | |
- (id)associatedObjectForKey:(NSString*)key { | |
NSMutableDictionary *dict = objc_getAssociatedObject(self, &associatedObjectsKey); | |
return [dict objectForKey:key]; | |
} | |
- (void)setAssociatedObject:(id)object forKey:(NSString*)key { | |
NSMutableDictionary *dict = objc_getAssociatedObject(self, &associatedObjectsKey); | |
if (!dict) { | |
dict = [[NSMutableDictionary alloc] init]; | |
objc_setAssociatedObject(self, &associatedObjectsKey, dict, OBJC_ASSOCIATION_RETAIN); | |
} [dict setObject:object forKey:key]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment