Search code examples
objective-cios5xcode4.2restkit

Getting "this class is not key value coding-compliant for the key id." when requesting RestKit loadObjectsAtResourcePath


I'm trying to deserialise JSON data into my iPhone application using RestKit's RKObjectManager.

My current issue is the app crashes with a :

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Course 0x6e71b10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'

when I call:

[manager loadObjectsAtResourcePath:@"/courses" delegate:nil];

My domain class - Course.h looks like

#import <Foundation/Foundation.h>



@interface Course : NSObject  {

}

@property(nonatomic) NSInteger *id;

@property(nonatomic, retain) NSString *name;

-(id)initWithIdAndName: (NSInteger *)inId inName:(NSString *)inName;
@end

and the Course.m looks like

#import "Course.h"
#import "NSDictionary+RKAdditions.h"


@implementation Course {

}
@synthesize name = _name;
@synthesize id = _id;

- (id)initWithIdAndName:(NSInteger *)inId inName:(NSString *)inName {
    _name = inName;
    _id = inId;
    return self;

}

#pragma mark NSCoding Protocol


- (void)encodeWithCoder:(NSCoder *)encoder;
{
[encoder encodeInteger:[self id] forKey:@"id"];
[encoder encodeObject:[self name] forKey:@"name"];
}

- (id)initWithCoder:(NSCoder *)decoder;
{
if ( ![super init] )
    return nil;

[self setId:[decoder decodeIntForKey:@"id"]];
[self setName:[decoder decodeObjectForKey:@"name"]];


return self;
}

@end 

The Json data retrieved from the server is

{"courses":[{"id":1,"name":"course 1"},{"id":2,"name":"course 2"}]}

And the RKObjectManager is invoked using the following code:

RKObjectMapping* courseMapping = [RKObjectMapping mappingForClass:[Course class]];
[courseMapping mapKeyPath:@"id" toAttribute:@"id"];
[courseMapping mapKeyPath:@"name" toAttribute:@"name"];


RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURL:@"http://firstbit/of/url"];

[manager.mappingProvider setMapping:courseMapping forKeyPath:@"courses"];
[manager loadObjectsAtResourcePath:@"/endpoint" delegate:nil];

Any ideas on this one?


Solution

  • It might help others, but I had this problem and the reason was that I was using NSInteger in my object definition. RestKit cannot map to NSInteger, only NSNumber.