mfanto
mfanto

Reputation: 14418

RestKit object mapping fails depending on property name

I'm seeing some strange behavior with RestKit and mapping an object. In my destination object, if I have an NSString * property named 'description', then the entire mapping fails. Changing this to any other name, or commenting it out, everything succeeds. This happens regardless if I have a map set up for the property, and I can reproduce it in simple test classes.

The JSON the server is returning:

{
    id = 1;
    name = item1;
},
    {
    id = 2;
    name = item2;
}

The object I want to map to, SimpleObject.h. Note that neither the description, nor the nonexistant property is in the JSON, meaning I don't think it's because 'description' is missing:

#import <Foundation/Foundation.h>

@interface SimpleObject : NSObject

@property (nonatomic, assign) NSInteger identifier;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *nonexistant;

@end

SimpleObject.m:

#import "SimpleObject.h"

@implementation SimpleObject

@synthesize identifier;
@synthesize name;
@synthesize description;
@synthesize nonexistant;

@end

I create the mapping in my AppDelegate:

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURLString:@"http://127.0.0.1/test"];

// Setup our object mappings
RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[SimpleObject class]];
[testMapping mapKeyPath:@"id" toAttribute:@"identifier"];
[testMapping mapKeyPath:@"name" toAttribute:@"name"];

 // Register our mappings with the provider using a resource path pattern 
[objectManager.mappingProvider setObjectMapping:testMapping forResourcePathPattern:@"/products"];

Inside my didLoadObjects method, I have:

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
    NSLog(@"Loaded products: %@", objects);

    SimpleObject *obj = [objects objectAtIndex:0];
    NSLog(@"Loaded product ID %d -> Name: %@ ", obj.identifier, obj.name);
}

This outputs:

Finished performing object mapping. Results: {
    "" =     (
        (null),
        (null)
    );
}

If I comment out the description property (but leave in nonexistant), then everything works:

Finished performing object mapping. Results: {
    "" =     (
        "<SimpleObject: 0x8a4d040>",
        "<SimpleObject: 0x8a50130>"
    );
}

Is there a reason the mapping is failing only for this property name? If I change the name to anything other than 'description', it also succeeds.

Upvotes: 0

Views: 263

Answers (1)

Rahul Wakade
Rahul Wakade

Reputation: 4805

It is conflicting with description property of NSObject. Rename it to other(like desc).

Upvotes: 2

Related Questions