2011-10-10 3 views
4

뉴스 항목을 제공하는 웹 서비스와 통신하기 위해 RestKit을 사용하려고합니다. 나는 내가 다음과 같은 로그 출력keyPath에 대한 객체 매핑을 찾을 수 없습니다 : ''

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 

그러나 그것은 나에게 많은 이해가되지 않습니다를 얻을이 코드

// 
// 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); 
} 

에게 서면에 문서의 this piece을 공부하고 기반으로했습니다. 문제가 무엇인지 알 수 있습니까?

답변

8

아래의 키 경로에 액세스하기 전에 setMapping:forKeyPath:으로 매핑을 설정해야한다고 생각합니다.

setMapping 메서드에서 @"news_items" 문자열을 사용했습니다. 그러나 JSON 피드에는 @"news_posts"이 표시됩니다.

+0

이것은 매우 당황 스럽습니다. 정말 고맙습니다. 문자열의 수정 내 문제를 해결. –

관련 문제