RESTkit unrecognized selector error
문제
I'm trying to add a person class via iPhone to my server.
I have the following code and I get this error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[Person object]: unrecognized selector sent to class 0x5fe6c' error
@interface Data : NSObject {
Person *person;
NSArray *dogs;
}
@property (nonatomic ,retain) Person *person;
@property (nonatomic ,retain) NSArray *dogs;
@end
@interface Person : NSObject {
NSString *name;
NSNumber *personId;
NSDate *updatedAt;
NSDate *createdAt;
}
@property (nonatomic , retain) NSDate * createdAt;
@property (nonatomic , retain) NSDate * updatedAt;
@property (nonatomic , retain) NSNumber *personId;
@property (nonatomic , retain) NSString *name;
@end
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[Person class]];
[userMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
[userMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];
[userMapping mapKeyPath:@"name" toAttribute:@"name"];
[userMapping mapKeyPath:@"id" toAttribute:@"personId"];
RKObjectMapping* dogMapping = [RKObjectMapping mappingForClass:[Dog class]];
[dogMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
[dogMapping mapKeyPath:@"person_id" toAttribute:@"spersonId"];
[dogMapping mapKeyPath:@"name" toAttribute:@"name"];
[dogMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];
[dogMapping mapKeyPath:@"id" toAttribute:@"dogId"];
RKObjectMapping *dataMapping = [RKObjectMapping mappingForClass:[Data class]];
[dataMapping mapKeyPath:@"dog" toAttribute:@"dogs"];
[dataMapping mapKeyPath:@"person" toRelationship:@"person" withMapping:userMapping];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:dataMapping];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/people"
objectMapping:dataMapping delegate:self];
-(void)createObject{
RKObjectRouter * router = [RKObjectManager sharedManager].router;
[router routeClass: [Person class] toResourcePath:@"/people/:personId"];
[router routeClass: [Person class] toResourcePath:@"/people"
forMethod:RKRequestMethodPOST];
Data *data =[[Data alloc]init];
Person *daveLiu = [Person object];
daveLiu.name = @"Dave Liu";
[[RKObjectManager sharedManager] postObject:daveLiu delegate:self];
}
해결책
Let's break it down:
Terminating app due to uncaught exception 'NSInvalidArgumentException'
OK, you have an invalid argument exception. Why?
reason: '+[Person object]:
A class method called object
was called on the Person class
unrecognized selector sent to class 0x5fe6c' error
... but the Person class doesn't implement the object
class method. Looking at your implementation of Person, we can see that this is the case.
So, where are we calling this from? A quick scan through mja's comments the code shows us this line:
Person *daveLiu = [Person object];
Which is probably your culprit. As mja says, this should probably be
Person *daveLiu = [Person alloc] init];