RESTkit 无法识别的选择器错误
我正在尝试通过 iPhone 将人员类别添加到我的服务器。
我有以下代码,但收到此错误:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[Person 对象]:无法识别的选择器发送到类 0x5fe6c”错误
@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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们来分解一下:
好的,您有一个无效的参数异常。为什么?
在 Person 类上调用了名为
object
的类方法...但 Person 类没有实现
object
类方法。查看您的 Person 实现,我们可以看到情况就是如此。那么,我们从哪里调用它呢?快速浏览
mja 的评论,代码向我们显示了这一行:这可能是你的罪魁祸首。正如 mja 所说,这可能应该是
Let's break it down:
OK, you have an invalid argument exception. Why?
A class method called
object
was called on the Person class... 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 commentsthe code shows us this line:Which is probably your culprit. As mja says, this should probably be