M16Model.m
1.69 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// M16Model.m
// M16
//
#import "M16Model.h"
@implementation M16Model
- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error
{
self = [super initWithDictionary:dictionaryValue error:error];
if (self == nil) return nil;
_m16Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
return self;
}
- (NSDictionary *)dictionaryValue {
NSMutableDictionary *modifiedDictionaryValue = [[super dictionaryValue] mutableCopy];
for (NSString *originalKey in [super dictionaryValue]) {
if ([self valueForKey:originalKey] == nil) {
[modifiedDictionaryValue removeObjectForKey:originalKey];
}
}
return [modifiedDictionaryValue copy];
}
#pragma mark - MTLJSONSerializing
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{};
}
#pragma mark - KVC method
/**
* 有的时候API的response会有空值,比如pubilshTime可能不是每次都有的,JSON是这样儿的:
* {
* "pubilshTime": null
* }
* Mantle在这种情况会将pubilshTime转换为nil,但如果是标量如NSInteger怎么办?KVC会直接raise NSInvalidArgumentException。
* 所以重写kvc的setNilValueForKey方法,设置为0
*/
- (void)setNilValueForKey:(NSString *)key
{
[self setValue:@0 forKey:key]; // For NSInteger/CGFloat/BOOL
}
- (id)valueForUndefinedKey:(NSString *)key
{
return nil;
}
#pragma mark - Date Formatter
+ (NSDateFormatter *)dateFormatter
{
static NSDateFormatter *_formatter;
if (!_formatter) {
_formatter = [[NSDateFormatter alloc] init];
_formatter.dateFormat = @"yyyy.MM.dd";
}
return _formatter;
}
@end