【问题标题】:EasyMapping fails to map int from AFNetworking responseEasyMapping 无法从 AFNetworking 响应映射 int
【发布时间】:2016-12-20 01:34:17
【问题描述】:

如果我第一次测试 EasyMapping,那么首先让我这样说,因此这是来自使用 Xcode 在创建新项目时提供的默认核心数据构造函数的测试项目。

当我尝试映射用户的初始登录时,我收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute:property = "userID"; desired type = NSNumber; given type = __NSArrayI; value = (
    12345
).'

错误是在尝试使用 AFNetworking 映射 POST 的结果时引起的:

Successfully logged in with responseObject: {
    collection =     {
        items =         (
                        {
                accountid = 12345;
                fname = John;
                lname = Smith;
            }
        );
    };
}

使用以下代码:

// Login server call and resulting Mapping
[[MyHTTPSessionManager sharedManager] POST:@"users" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"Successfully logged in with responseObject: %@", responseObject);
    NSManagedObjectContext *context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];

    // Start EasyMapping
    User *user = [EKManagedObjectMapper objectFromExternalRepresentation:responseObject withMapping:[User objectMapping] inManagedObjectContext:context];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];

User.m 对象映射函数

+ (EKManagedObjectMapping*)objectMapping
{
    return [EKManagedObjectMapping mappingForEntityName:@"User" withRootPath:@"collection.items" withBlock:^(EKManagedObjectMapping * _Nonnull mapping) {
        [mapping mapPropertiesFromDictionary:@{
                                           @"accountid" : @"userID",
                                           @"fname"     : @"firstName",
                                           @"lname"     : @"lastName",
                                           }];
        [mapping setPrimaryKey:@"userID"];
    }];
}

Core 数据设置为自动生成以下 User+CoreDataProperties.h 文件:

#import "User.h"

NS_ASSUME_NONNULL_BEGIN

@interface User (CoreDataProperties)

@property (nullable, nonatomic, retain) NSNumber *userID;
@property (nullable, nonatomic, retain) NSString *firstName;
@property (nullable, nonatomic, retain) NSString *lastName;

@end

NS_ASSUME_NONNULL_END

我不确定我是否在这里遗漏了一些基本的东西......我正在使用我的 Podfile 中描述的以下 API 版本:

pod 'AFNetworking', '~> 3.1'
pod 'EasyMapping', '~> 0.18'

如果我遗漏了任何内容,或者您​​对我的设置方式有任何疑问,请告诉我

【问题讨论】:

    标签: ios json mapping


    【解决方案1】:

    问题似乎与服务器提供的嵌套 JSON 有关。

    如果我更改该行以仅传递 JSON 对象 items 持有的数组,则错误似乎已解决。

    User *user = [EKManagedObjectMapper objectFromExternalRepresentation:[[(NSDictionary*)responseObject objectForKey:@"collection"] objectForKey:@"items"] withMapping:[User objectMapping] inManagedObjectContext:context];
    

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多