Ios EasyMapping无法从AFNetworking响应映射int
如果我第一次测试EasyMapping,那么我首先要说的是,这是一个测试项目,在创建新项目时使用Xcode提供的默认核心数据构造函数 当我尝试映射用户的初始登录名时,会出现以下错误:Ios EasyMapping无法从AFNetworking响应映射int,ios,json,mapping,Ios,Json,Mapping,如果我第一次测试EasyMapping,那么我首先要说的是,这是一个测试项目,在创建新项目时使用Xcode提供的默认核心数据构造函数 当我尝试映射用户的初始登录名时,会出现以下错误: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute:property = "userID"; desired typ
*** 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 objectMapping功能
+ (EKManagedObjectMapping*)objectMapping
{
return [EKManagedObjectMapping mappingForEntityName:@"User" withRootPath:@"collection.items" withBlock:^(EKManagedObjectMapping * _Nonnull mapping) {
[mapping mapPropertiesFromDictionary:@{
@"accountid" : @"userID",
@"fname" : @"firstName",
@"lname" : @"lastName",
}];
[mapping setPrimaryKey:@"userID"];
}];
}
核心数据设置为自动生成以下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
我不确定我是否遗漏了一些基本的东西。。。我正在使用以下API版本,如我的pod文件中所述:
pod 'AFNetworking', '~> 3.1'
pod 'EasyMapping', '~> 0.18'
如果我遗漏了什么,或者您对我如何设置东西有任何疑问,请告诉我。问题似乎与服务器提供的嵌套JSON有关 如果我将行更改为只传递JSON对象
items
所持有的数组,则错误似乎已得到解决
User *user = [EKManagedObjectMapper objectFromExternalRepresentation:[[(NSDictionary*)responseObject objectForKey:@"collection"] objectForKey:@"items"] withMapping:[User objectMapping] inManagedObjectContext:context];
问题似乎与服务器提供的嵌套JSON有关 如果我将行更改为只传递JSON对象
items
所持有的数组,则错误似乎已得到解决
User *user = [EKManagedObjectMapper objectFromExternalRepresentation:[[(NSDictionary*)responseObject objectForKey:@"collection"] objectForKey:@"items"] withMapping:[User objectMapping] inManagedObjectContext:context];