-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSCache+sdfextrasubs.m
45 lines (35 loc) · 1.34 KB
/
NSCache+sdfextrasubs.m
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
//
// NSCache+sdfextrasubs.m
//
// Created by Steven Fisher on 2012-09-05.
//
// This is public domain. Use as you see fit.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import "NSCache+sdfextrasubs.h"
@implementation NSCache(sdfextrasubs)
#ifdef __IPHONE_6_0
#define CAST_TO_BLOCK id
#else
#define CAST_TO_BLOCK __bridge void *
#endif
+ (void)load {
// Each category's +load is called on startup. This method installs our getter/setter if NSCache doesn't have one.
// We only install our custom getter if no getter exists.
SEL getter = @selector(objectForKeyedSubscript:);
if ( ![self instancesRespondToSelector: getter]) {
IMP imp = imp_implementationWithBlock( (CAST_TO_BLOCK)^NSObject *(NSCache *self, id subscript){
return [self objectForKey:subscript]; // replace with your getter
});
class_addMethod(self, getter, imp, "@@:@");
}
// We only install our custom setter if no setter exists.
SEL setter = @selector(setObject:forKeyedSubscript:);
if ( ![self instancesRespondToSelector: setter]) {
IMP imp = imp_implementationWithBlock( (CAST_TO_BLOCK)^void(NSCache *self, id object, id<NSCopying> subscript){
[self setObject:object forKey: (NSString *)subscript];
});
class_addMethod(self, setter, imp, "v@:@@");
}
}
@end