RestKit:serializedObjectForMIMEType 失败并出现错误

发布于 2024-12-23 06:19:36 字数 1262 浏览 0 评论 0原文

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

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

发布评论

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

评论(1

风追烟花雨 2024-12-30 06:19:36

我找到了这个链接:
使用示例

在每个示例中我都可以找到映射是用 [NSDictionary class] 或 [NSMutableDictionary class] 完成的

所以也许是这样的:

RKObjectMapping* phoneMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class] ];
[phoneMapping  mapAttributes:@"number", @"tag", nil];

 RKObjectMapping *contactMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[contactMapping mapKeyPath:@"name" toAttribute:@"name"];
[contactMapping mapKeyPath:@"phoneList" toRelationship:@"phoneList" withMapping:phoneMapping];

您想要这个序列化字符串与 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 :

RKObjectMapping* phoneMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class] ];
[phoneMapping  mapAttributes:@"number", @"tag", nil];

 RKObjectMapping *contactMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[contactMapping mapKeyPath:@"name" toAttribute:@"name"];
[contactMapping mapKeyPath:@"phoneList" toRelationship:@"phoneList" withMapping:phoneMapping];

You want this serialized string for using with RKClient ?

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