RestKit:serializedObjectForMIMEType 失败并出现错误
我正在尝试使用 RestKit 从我的自定义对象中获取 JSON 格式字符串。
这是我的代码:
@interface MyPhoneModel : NSObject
@property (nonatomic, copy) NSString *number;
@property (nonatomic, copy) NSString *tag;
@end
@interface MyContactModel : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSArray *phoneList;
@end
- (NSString *)jsonString:(MyContactModel *)contact
{
RKObjectMapping *phoneMapping = [RKObjectMapping mappingForClass:[MyPhoneModel class]];
[phoneMapping mapKeyPath:@"number" toAttribute:@"number"];
[phoneMapping mapKeyPath:@"tag" toAttribute:@"tag"];
RKObjectMapping *contactMapping = [RKObjectMapping mappingForClass:[MyContactModel class]];
[contactMapping mapKeyPath:@"name" toAttribute:@"name"];
[contactMapping mapKeyPath:@"phoneList" toRelationship:@"phoneList" withMapping:phoneMapping];
NSError *error = nil;
NSString *json = [[RKObjectSerializer serializerWithObject:contact mapping:contactMapping] serializedObjectForMIMEType:RKMIMETypeJSON error:&error];
return json;
}
但是当我调用这个方法时,它失败并出现错误。 错误域=JKErrorDomain代码=-1“无法序列化对象类MyPhoneModel。” UserInfo=0x898c400 {NSLocalizedDescription=无法序列化对象类MyPhoneModel。
那么,如何解决这个问题呢?
I'm trying to get the JSON format string from my custom object by using RestKit.
Here is my code:
@interface MyPhoneModel : NSObject
@property (nonatomic, copy) NSString *number;
@property (nonatomic, copy) NSString *tag;
@end
@interface MyContactModel : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSArray *phoneList;
@end
- (NSString *)jsonString:(MyContactModel *)contact
{
RKObjectMapping *phoneMapping = [RKObjectMapping mappingForClass:[MyPhoneModel class]];
[phoneMapping mapKeyPath:@"number" toAttribute:@"number"];
[phoneMapping mapKeyPath:@"tag" toAttribute:@"tag"];
RKObjectMapping *contactMapping = [RKObjectMapping mappingForClass:[MyContactModel class]];
[contactMapping mapKeyPath:@"name" toAttribute:@"name"];
[contactMapping mapKeyPath:@"phoneList" toRelationship:@"phoneList" withMapping:phoneMapping];
NSError *error = nil;
NSString *json = [[RKObjectSerializer serializerWithObject:contact mapping:contactMapping] serializedObjectForMIMEType:RKMIMETypeJSON error:&error];
return json;
}
But when I call this method, it failed with error.
Error Domain=JKErrorDomain Code=-1 "Unable to serialize object class MyPhoneModel." UserInfo=0x898c400 {NSLocalizedDescription=Unable to serialize object class MyPhoneModel.
So, how to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了这个链接:
使用示例
在每个示例中我都可以找到映射是用 [NSDictionary class] 或 [NSMutableDictionary class] 完成的
所以也许是这样的:
您想要这个序列化字符串与 RKClient 一起使用吗?
I found this link :
Examples of using
In every example I could find the mapping is done with [NSDictionary class] or [NSMutableDictionary class]
So maybe like this :
You want this serialized string for using with RKClient ?