forked from fortinmike/XcodeBoost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSObject+XcodeBoost.m
More file actions
51 lines (40 loc) · 1.23 KB
/
NSObject+XcodeBoost.m
File metadata and controls
51 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// NSObject+XcodeBoost.m
// XcodeBoost
//
// Created by Michaël Fortin on 2013-09-25.
// Copyright (c) 2013 Michaël Fortin. All rights reserved.
//
#import "NSObject+XcodeBoost.h"
#import <objc/message.h>
@implementation NSObject (XcodeBoost)
static const char KeysStorageKey[] = "keysStorage";
- (void)xb_setAssociatedObject:(id)object forKey:(NSString *)key
{
objc_setAssociatedObject(self, [self xb_keyForString:key], object, OBJC_ASSOCIATION_RETAIN);
}
- (void)xb_removeAssociatedObjectForKey:(NSString *)key
{
objc_setAssociatedObject(self, [self xb_keyForString:key], nil, OBJC_ASSOCIATION_ASSIGN);
}
- (id)xb_associatedObjectForKey:(NSString *)key
{
return objc_getAssociatedObject(self, [self xb_keyForString:key]);
}
- (void *)xb_keyForString:(NSString *)string
{
NSMutableDictionary *keysStorage = objc_getAssociatedObject(self, &KeysStorageKey);
if (keysStorage == nil)
{
keysStorage = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &KeysStorageKey, keysStorage, OBJC_ASSOCIATION_RETAIN);
}
NSString *existingKey = [keysStorage objectForKey:string];
if (existingKey == nil)
{
existingKey = string;
[keysStorage setObject:string forKey:string];
}
return (__bridge void *)(existingKey);
}
@end