Heia Heia - 1 year ago 202
iOS Question

EasyMapping fails to map int from AFNetworking response

Let me start by saying this if my fist time testing out EasyMapping and as such this is from a test project using the default Core Data constructors provided by Xcode when making a new Project.

When I try to map an initial login of a user I get the following error:

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

The error is caused when trying to map the result of a POST using AFNetworking:

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

Using the following code:

// 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 fucntion

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

The Core data is setup such that it auto generates the following User+CoreDataProperties.h file:

#import "User.h"


@interface User (CoreDataProperties)

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



I'm not sure if I'm missing something basic here or not... I am using the following API versions as described in my Podfile:

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

Let me know if I'm missing anything or if you have any questions on how I have things setup

Answer Source

The issue appears to be around the nested JSON given by the server.

If I change the line to pass just the array held by the JSON object items the error seems be resolved.

User *user = [EKManagedObjectMapper objectFromExternalRepresentation:[[(NSDictionary*)responseObject objectForKey:@"collection"] objectForKey:@"items"] withMapping:[User objectMapping] inManagedObjectContext:context];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download