找不到 keyPath 的对象映射:''
我正在尝试使用 RestKit 与提供新闻项目的网络服务进行通信。我研究了这篇文档并基于编写这段代码时,
//
// NewsViewController_iPhone.m
#import "NewsViewController_iPhone.h"
#import <RestKit/RestKit.h>
#import <RestKit/CoreData/CoreData.h>
#import "NewsPost.h"
@implementation NewsViewController_iPhone
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://192.168.0.197:3000"];
RKObjectMapping* newsPostMapping = [RKObjectMapping mappingForClass:[NewsPost class]];
[newsPostMapping mapKeyPath:@"title" toAttribute:@"title"];
[newsPostMapping mapKeyPath:@"body" toAttribute:@"body"];
[newsPostMapping mapKeyPath:@"image" toAttribute:@"image"];
[newsPostMapping mapKeyPath:@"date" toAttribute:@"date"];
[manager.mappingProvider setMapping:newsPostMapping forKeyPath:@"news_items"];
[manager loadObjectsAtResourcePath:@"/news.json"delegate:self];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
NSString *stringError = @"Ett problem uppstod vid hämtning av nyheter";
NSLog(stringError);
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:stringError];
[alert show];
[alert release];*/
}
- (void)request:(RKRequest*)request didLoadResponse:
(RKResponse*)response {
if ([request isGET]) {
if ([response isOK]) {
NSLog(@"Retrieved JSON: %@", [response bodyAsString]);
}
} else if ([request isPOST]) {
if ([response isJSON]) {
NSLog(@"Got a JSON response back from our POST!");
}
}
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"%@", objects);
}
我得到以下日志输出
2011-10-10 15:57:40.807 App[94979:207] Retrieved JSON: {"news_posts":[{"title":"test title","body":"test body","image":"/uploads/post/image/1/app_iPhone.png","date":"10/10 2011"}]}
2011-10-10 15:57:40.808 App[94979:7507] W restkit.object_mapping:RKObjectMapper.m:74 Adding mapping error: Could not find an object mapping for keyPath: ''
2011-10-10 15:57:40.809 App[94979:7507] E restkit.network:RKObjectLoader.m:190 Encountered errors during mapping: Could not find an object mapping for keyPath: ''
2011-10-10 15:57:40.809 App[94979:7507] Ett problem uppstod vid hämtning av nyheter
,但这对我来说没有多大意义。有人知道可能是什么问题吗?
I am trying to use RestKit for communicating with a web service providing news items. I've studied this piece of documentation and based on that written this code
//
// NewsViewController_iPhone.m
#import "NewsViewController_iPhone.h"
#import <RestKit/RestKit.h>
#import <RestKit/CoreData/CoreData.h>
#import "NewsPost.h"
@implementation NewsViewController_iPhone
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://192.168.0.197:3000"];
RKObjectMapping* newsPostMapping = [RKObjectMapping mappingForClass:[NewsPost class]];
[newsPostMapping mapKeyPath:@"title" toAttribute:@"title"];
[newsPostMapping mapKeyPath:@"body" toAttribute:@"body"];
[newsPostMapping mapKeyPath:@"image" toAttribute:@"image"];
[newsPostMapping mapKeyPath:@"date" toAttribute:@"date"];
[manager.mappingProvider setMapping:newsPostMapping forKeyPath:@"news_items"];
[manager loadObjectsAtResourcePath:@"/news.json"delegate:self];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
NSString *stringError = @"Ett problem uppstod vid hämtning av nyheter";
NSLog(stringError);
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:stringError];
[alert show];
[alert release];*/
}
- (void)request:(RKRequest*)request didLoadResponse:
(RKResponse*)response {
if ([request isGET]) {
if ([response isOK]) {
NSLog(@"Retrieved JSON: %@", [response bodyAsString]);
}
} else if ([request isPOST]) {
if ([response isJSON]) {
NSLog(@"Got a JSON response back from our POST!");
}
}
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
NSLog(@"%@", objects);
}
I get the following log output
2011-10-10 15:57:40.807 App[94979:207] Retrieved JSON: {"news_posts":[{"title":"test title","body":"test body","image":"/uploads/post/image/1/app_iPhone.png","date":"10/10 2011"}]}
2011-10-10 15:57:40.808 App[94979:7507] W restkit.object_mapping:RKObjectMapper.m:74 Adding mapping error: Could not find an object mapping for keyPath: ''
2011-10-10 15:57:40.809 App[94979:7507] E restkit.network:RKObjectLoader.m:190 Encountered errors during mapping: Could not find an object mapping for keyPath: ''
2011-10-10 15:57:40.809 App[94979:7507] Ett problem uppstod vid hämtning av nyheter
But it does not make much sense to me. Does anybody know what the problem could be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须在访问其下面的关键路径之前使用
setMapping:forKeyPath:
设置映射。此外,您还在
setMapping
方法中使用了字符串@"news_items"
。但是,在您的 JSON feed 中,它显示@"news_posts"
。I think you have to set the mapping with
setMapping:forKeyPath:
before accessing the key paths below it.Also, you have used the string
@"news_items"
in yoursetMapping
method. However, in your JSON feed it says@"news_posts"
.