RESTkit 无法识别的选择器错误

发布于 2024-12-26 04:09:34 字数 2254 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2025-01-02 04:09:34

让我们来分解一下:

由于未捕获的异常“NSInvalidArgumentException”而终止应用

好的,您有一个无效的参数异常。为什么?

原因:'+[Person 对象]:

在 Person 类上调用了名为 object 的类方法

无法识别的选择器发送到类 0x5fe6c' 错误

...但 Person 类没有实现 object 类方法。查看您的 Person 实现,我们可以看到情况就是如此。

那么,我们从哪里调用它呢?快速浏览 mja 的评论,代码向我们显示了这一行:

Person *daveLiu = [Person object];

这可能是你的罪魁祸首。正如 mja 所说,这可能应该是

Person *daveLiu = [Person alloc] init];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文