-
Notifications
You must be signed in to change notification settings - Fork 2
/
NSObject+Additions.m
107 lines (79 loc) · 2.8 KB
/
NSObject+Additions.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// NSObject+Additions.m
// Additions
//
// Created by jlopez on 2/27/10.
// Copyright 2010 JLA. All rights reserved.
//
#import "NSObject+Additions.h"
#import <objc/runtime.h>
@implementation NSObject (JLAdditions)
- (NSDate *)dateValueForKey:(NSString *)key {
id obj = [self objectValueForKey:key];
if (obj == nil)
return nil;
if ([obj isKindOfClass:[NSDate class]])
return obj;
return [NSDate dateFromISO8601String:[NSString stringWithValue:obj]];
}
- (NSDate *)dateValueForKeyPath:(NSString *)keyPath {
id obj = [self objectValueForKeyPath:keyPath];
if (obj == nil)
return nil;
if ([obj isKindOfClass:[NSDate class]])
return obj;
return [NSDate dateFromISO8601String:[NSString stringWithValue:obj]];
}
- (NSInteger)integerValueForKey:(NSString *)key {
return [[self stringValueForKey:key] integerValue];
}
- (NSInteger)integerValueForKeyPath:(NSString *)keyPath {
return [[self stringValueForKeyPath:keyPath] integerValue];
}
- (NSNumber *)numberValueForKey:(NSString *)key {
return [NSNumber numberWithValue:[self valueForKey:key]];
}
- (NSNumber *)numberValueForKeyPath:(NSString *)keyPath {
return [NSNumber numberWithValue:[self valueForKeyPath:keyPath]];
}
- (NSString *)stringValueForKey:(NSString *)key {
return [NSString stringWithValue:[self valueForKey:key]];
}
- (NSString *)stringValueForKeyPath:(NSString *)keyPath {
return [NSString stringWithValue:[self valueForKeyPath:keyPath]];
}
- (id)objectValueForKey:(NSString *)key {
id object = [self valueForKey:key];
return object == [NSNull null] ? nil : object;
}
- (id)objectValueForKeyPath:(NSString *)keyPath {
id object = [self valueForKeyPath:keyPath];
return object == [NSNull null] ? nil : object;
}
static NSMutableDictionary *propertiesForClassDictionary = nil;
+ (NSArray *)arrayWithPropertyNames {
id allKeys = [propertiesForClassDictionary objectForKey:self];
if (allKeys == nil) {
if (propertiesForClassDictionary == nil)
propertiesForClassDictionary = [[NSMutableDictionary alloc] initWithCapacity:64];
NSUInteger outCount;
objc_property_t *properties = class_copyPropertyList(self, &outCount);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:outCount];
for(NSUInteger i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if(propName) {
NSString *propertyName = [NSString stringWithUTF8String:propName];
[array addObject:propertyName];
}
}
free(properties);
NSArray *superclassKeys = [[self superclass] arrayWithPropertyNames];
if (superclassKeys)
[array addObjectsFromArray:superclassKeys];
allKeys = [NSArray arrayWithArray:array];
[propertiesForClassDictionary setObject:allKeys forKey:self];
}
return allKeys;
}
@end