Mantle使用问题: MTLTransformerErrorHandlingErrorDomain

再使用Mantle序列化JSON文件的时候程序一直crash,使用断点判断到获取model却一直为nil,然后把这个空对象add到数组里的时候就崩了。

for (NSDictionary *attributes in responsearray) {
            NSError *error = nil;
            BookDetailsModel *model = [MTLJSONAdapter modelOfClass:[BookDetailsModel class] fromJSONDictionary:attributes error:&error];
            DLog(@"-->%@",error.description);
            [mutablearray addObject:model];
        }
        

Error description:

Domain=MTLTransformerErrorHandlingErrorDomain Code=1 “Value did not match expected type” UserInfo={MTLTransformerErrorHandlingInputValueErrorKey=7, NSLocalizedDescription=Value did not match expected type, NSLocalizedFailureReason=Expected 7 to be of class NSNumber but got NSTaggedPointerString}

大意也就是说声明的NSNumber类型变量不能匹配到相应的类型。一些数据格式如下:

从服务器端拉倒数据之后JSON信息:

 {
        "title": "北师版三年级上册语文",
        "grade": "",
        "image_id": 8821,
        "edition": "北师大版电子课本",
        "mp3_path": "http://file.xxx.com/static/server/tom61_kejian/598/99/mp3/9.mp3",
        "path": "http://file.xxx.com/static/server/tom61_kejian/907/106/img/7.jpg",
        "lesson": null,
        "page": "7",
        "subject": ""
    },
    {
        "title": "北师版三年级上册语文",
        "grade": "",
        "image_id": 8847,
        "edition": "北师大版电子课本",
        "mp3_path": "",
        "path": "http://file.xxx.com/static/server/tom61_kejian/907/106/img/8.jpg",
        "lesson": null,
        "page": "8",
        "subject": ""
    }

客户端获取数据后放到数组里,数组里的字典格式(不理解为什么变量有的双引号有的没有,和在网页上获取内容不全一样,双引号问题):

{
edition = "\U5317\U5e08\U5927\U7248\U7535\U5b50\U8bfe\U672c";
grade = "";
"image_id" = 8821;
lesson = "<null>";
"mp3_path" = "http://file.xxx.com/static/server/tom61_kejian/598/99/mp3/9.mp3";
page = 7;
path = "http://file.xxx.com/static/server/tom61_kejian/907/106/img/7.jpg";
subject = "";
title = "\U5317\U5e08\U7248\U4e09\U5e74\U7ea7\U4e0a\U518c\U8bed\U6587";
},
{
edition = "\U5317\U5e08\U5927\U7248\U7535\U5b50\U8bfe\U672c";
grade = "";
"image_id" = 8847;
lesson = "<null>";
"mp3_path" = "";
page = 8;
path = "http://file.xxx.com/static/server/tom61_kejian/907/106/img/8.jpg";
subject = "";
title = "\U5317\U5e08\U7248\U4e09\U5e74\U7ea7\U4e0a\U518c\U8bed\U6587";
}
// .h 文件
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* grade;
@property (nonatomic, strong) NSNumber* image_id;
@property (nonatomic, copy) NSString* edition;
@property (nonatomic, copy) NSString* mp3_path;
@property (nonatomic, copy) NSString* path;
@property (nonatomic, copy) NSString* lesson;
@property (nonatomic, strong) NSNumber* page;
@property (nonatomic, copy) NSString* subject;

// .m 文件
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"title": @"title",
@"grade": @"grade",
@"image_id": @"image_id",
@"edition": @"edition",
@"mp3_path": @"mp3_path",
@"path": @"path",
@"lesson": @"lesson",
@"page": @"page",
@"subject": @"subject"
};
}

搞了半天,就在我想Mantle的Github repo上添加issue的时候发现,服务器端返回的数据时候:”image_id” = 8821; “mp3_path” = “”; 前面说到的变量的有双引号有的没有,然后和后台沟通也没查出什么问题,因为在postman上获取接口信息的时候根本没问题。

然后呢,我就开始注释.m文件里代码,让他一个个变量的获取,看看是在获取那个变量的时候出问题,果然查出来是 page变量的问题,服务前返回的是String类型,因为是数据接口潜意识写成NSNumer类型来接受。改成@property (nonatomic, copy) NSNumber* page;就没有问题了。好吧归根结底还是命名规范的问题。


####后记

在使用Mantle的时候还是会有一些其他的坑存在,像数据类型不要用NSInteger、int,最好用NSNumer接受,如果服务器端返回的数据为nil,程序就crash掉了。issue的问题https://github.com/Mantle/Mantle/issues/595,还有一些其他坑在,如:接受NSURL对象,但是URL字符串包含中文https://duxinfeng.com/2015/07/26/Mantle%E7%9A%84%E4%BD%BF%E7%94%A8