微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 错误:’无法为keyPath新闻添加映射,一个已经存在…’

我正在使用RestKit从Restful Web Service获取数据.使用平面文件,它可以很好地工作.如果我想获得嵌套 JSON的响应,我的问题就开始了.

我的JSON看起来像:

[{"homename":"Alien","created_at":"2011-09-15T12:46:37Z","updated_at":"2011-09-15T12:46:37Z","gametype":"Final match","id":1,"date":"2016-10-10","guestname":"Predator","news":[{"minute":null,"created_at":"2011-09-15T13:19:51Z","player":null,"title":"Title","updated_at":"2011-09-15T13:19:51Z","bodytext":"News","game_id":1},{"minute":null,"created_at":"2011-09-15T13:22:06Z","title":"New news","updated_at":"2011-09-15T13:22:06Z","id":2,"bodytext":"Old socks","created_at":"2011-09-15T13:26:32Z","title":"another title","updated_at":"2011-09-15T13:26:32Z","id":3,"bodytext":"Bodytext 2","created_at":"2011-09-22T12:35:19Z","title":"comment","updated_at":"2011-09-22T12:35:19Z","id":4,"bodytext":"body of the comment","game_id":1}]}]

使用以下代码,我想获取映射的数据.

RKLogConfigureByName("RestKit/Network*",RKLogLevelTrace);
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://X.X.X.X:3000"];

// Enable automatic network activity indicator management
//objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;


RKObjectMapping* newsMapping = [RKObjectMapping mappingForClass:[News class]];
[newsMapping mapKeyPath:@"id" toAttribute:@"id"];
[newsMapping mapKeyPath:@"minute" toAttribute:@"minute"];
[newsMapping mapKeyPath:@"title" toAttribute:@"title"];
[newsMapping mapKeyPath:@"bodytext" toAttribute:@"bodytext"];

// Setup our object mappings
RKObjectMapping* gameMapping = [RKObjectMapping mappingForClass:[Game class]];
//  [gameMapping mapKeyPath:@"id" toAttribute:@"id"];

[gameMapping mapKeyPath:@"guestname" toAttribute:@"guestname"];
[gameMapping mapKeyPath:@"homename" toAttribute:@"homename"];
[gameMapping mapKeyPath:@"date" toAttribute:@"date"];
[gameMapping mapKeyPath:@"gametype" toAttribute:@"gametype"];
[gameMapping mapKeyPath:@"news" toAttribute:@"news"];

[gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping];
[objectManager.mappingProvider setMapping:gameMapping forKeyPath:@"games"];

该声明
[gameMapping mapKeyPath:@“news”toRelationship:@“news”withMapping:newsMapping];
在运行时引发异常,我无法弄清楚原因:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason: 'Unable to add mapping for keyPath news,one already exists...'

有人看到我做错了吗? 1:n关系的关系是否错误

解决方法

新闻keyPath有两个映射:
[gameMapping mapKeyPath:@"news" toAttribute:@"news"];

[gameMapping mapKeyPath:@"news" toRelationship:@"news" withMapping:newsMapping];

删除一个,看看它是否有帮助.

干杯.

原文地址:https://www.jb51.cc/iOS/332006.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐