/
JTURLMappings.m
63 lines (44 loc) · 1.17 KB
/
JTURLMappings.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
//
// NSURL+JTURLMapping.m
// JTObjectMapping
//
// Created by Vladimir Shevchenko on 11/14/13.
//
//
#import "JTURLMappings.h"
@interface JTURLMappings : NSObject <JTValidMappingKey>
@property (nonatomic, copy) NSString *key;
@end
@implementation JTURLMappings
+ (id <JTValidMappingKey>)mappingWithKey:(NSString *)key
{
JTURLMappings *map = [[JTURLMappings alloc] init];
map.key = key;
return [map autorelease];
}
- (void)dealloc {
self.key = nil;
[super dealloc];
}
- (BOOL)transformValue:(NSObject *)oldValue
toValue:(NSObject **)newValue
forKey:(NSString **)key {
if ([oldValue isKindOfClass:[NSString class]]) {
NSURL *url = [NSURL URLWithString:(NSString *)oldValue];
*newValue = url;
*key = self.key;
return (url != nil);
} else if ([oldValue isKindOfClass:[NSNull class]]) {
*newValue = nil;
*key = self.key;
return YES;
}
return NO;
}
@end
@implementation NSURL (JTURLMappings)
+ (id <JTValidMappingKey>)mappingWithKey:(NSString *)key
{
return [JTURLMappings mappingWithKey:key];
}
@end