matthewcheok / Realm-JSON

A concise Mantle-like way of working with Realm and JSON.
MIT License
661 stars 129 forks source link

crash with the following json #84

Open xujiajia opened 8 years ago

xujiajia commented 8 years ago

json

{
    "list": [
        {
            "liveid": "23",
            "name": "CCTV1",
            "catid": "1",
            "currentlive": {
                "id": "227670",
                "channelid": "23"
            },
            "nextlive": {
                "id": "227671",
                "channelid": "23"
            }
        },
        {
            "liveid": "73",
            "name": "cctv1-2m",
            "catid": "1",
            "currentlive": "",
            "nextlive": ""
        }
    ]
}

Model:LiveChannelList

@interface LiveChannelList : RLMObject

@property NSString * catid;
@property LiveChannelInfo * currentlive;
@property NSString * liveid;
@property NSString * name;
@property LiveChannelInfo * nextlive;

@end

RLM_ARRAY_TYPE(LiveChannelList)

@implementation LiveChannelList

+(NSString *)primaryKey{

    return @"liveid";
}

+ (NSDictionary *)JSONInboundMappingDictionary {

    return @{
             @"liveid": @"liveid",
             @"name": @"name",
             @"catid": @"catid",
             @"currentlive": @"currentlive",
             @"nextlive": @"nextlive"
             };
}
@end

Model:LiveChannelInfo

@interface LiveChannelInfo : RLMObject

@property  NSString * idField;
@property  NSString * channelname;

@end

@implementation LiveChannelInfo

+ (NSDictionary *)JSONInboundMappingDictionary {

    return @{
             @"id": @"idField",
             @"channelid": @"channelid"
             };
}

@end

crash ,because the second data in json , currentlive and nextlive ,their valus is "" I try to resolve ,but ...

viktorasl commented 8 years ago

Hey. It's not quite a valid logic that currentlive and nextlive might carry either object or empty string. Making it a nil as a default value should solve your problem.