111iOS 常见问题汇总

iOS 解决服务器返回字段可能是数组可以是字符串空类型崩溃

2017-08-08  本文已影响162人  翻滚的炒勺2013

先看这个json,注意imgs字段

responseObject{
    code = 1;
    data =     {
        day = "2017-08-08";
        end =         {
            address = "\U5317\U4eac\U5e02\U660c\U5e73\U533a\U9f99\U6cfd\U56ed\U8857\U9053\U9ec4\U5e73\U8def3\U53f7\U9f99\U8dc3\U82d1\U4e1c5\U533a";
            imgs =             (
                "http://www.ace.eiboran.net/Uploads/appkq/59892113a15e1.png"
            );
            other = "\U53d8";
            status = 1;
            tags =             (
                "\U65e9\U9000",
                "\U5185\U52e4"
            );
            times = "10:25:23";
            timesf = "(\U65e9\U90007\U5c0f\U65f634\U5206\U949f37\U79d2)";
        };
        endtime = "18:00";
        start =         {
            address = "\U5317\U4eac\U5e02\U6d77\U6dc0\U533a\U7f8a\U574a\U5e97\U8857\U9053\U897f\U4e09\U73af\U4e2d\U8def19\U53f7\U9662\U4e2d\U533a168\U53f7\U697c";
            imgs = "";
            other = Asdasdasd;
            status = 1;
            tags =             (
                "\U8fdf\U5230",
                "\U5185\U52e4"
            );
            times = "09:08:34";
            timesf = "(\U8fdf\U52308\U5206\U949f34\U79d2)";
        };
        starttime = "09:00";
    };
    message = success;
}

一个数组类型一个是空字符串,这个时候如果解析的时候不做判断是会崩溃的

在这里我是这样处理的判断他的类型,如果是空字符串就什么都不做如果是数组那就就解析

 NSArray *iamgArray = dict[@"imgs"];
                        if ([iamgArray isKindOfClass:[NSString class]]) {
                            //                        dataModel.imgs addObject:@""
                            
                        } else {
                            if (iamgArray.count > 0) {
                                
                                for (NSString *url in dict[@"imgs"]) {
                                    [dataModel.imageArray addObject:url];
                                }
                            }
                        }
上一篇下一篇

猜你喜欢

热点阅读