M16Model.m 1.69 KB
//
//  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