2012-12-30 49 views
2

所以我想從我的鐵軌搶JSON信息應用與RestKit的Rails 3.2.8 + RestKit - 映射不KVC

我的代碼這樣做,是像這樣:

應用代表

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    //Initialise RestKit 
    NSURL *URL = [NSURL URLWithString:@"https://myapp.dev"]; 
    AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:URL]; 

    //Enable Activity Indicator Spinner 
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; 


    [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON]; 

    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; 


    RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Info class]]; 

    [infoMapping addAttributeMappingsFromDictionary:@{ 
    @"sample":@"sample", 
    @"sample_1":@"sample_1" 
    }]; 

    RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping 
                         pathPattern:@"/info" 
                         keyPath:nil 
                         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

    [objectManager addResponseDescriptor:infoDescriptor]; 
} 

查看文件

- (void)loadInfo 
{ 

    RKObjectManager *objectManager = [RKObjectManager sharedManager]; 

    [objectManager getObjectsAtPath:@"/info" 
         parameters:nil 
          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

           NSArray *info = [mappingResult array]; 

           NSLog(@"Loaded info: %@", info); 


           _info = info; 


          } failure:^(RKObjectRequestOperation *operation, NSError *error) { 

           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting into" 
                       message:[error localizedDescription] 
                       delegate:nil 
                       cancelButtonTitle:@"OK" 
                       otherButtonTitles:nil]; 
           [alert show]; 
           NSLog(@"Hit error: %@", error); 

          }]; 
} 

問題是。在日誌輸出上,RestKit告訴我它成功映射了所有的東西。但後來當我嘗試用兩種方法來查看對象登錄視圖文件,並使用po調試與我得到以下

374 Finished performing object mapping. Results: { 
    "<null>" = "<Info: 0xa291b30>"; 
} 

我無法查看對象,並使用斷點它顯示爲:

Object exists but cannot view it

我一直在掙扎這幾天,我不知道什麼嘗試。任何幫助將不勝感激

回答

2

我遇到了類似的問題。我不太清楚爲什麼,但當我做了以下修復它。

- (void)loadInfo 
{ 

    RKObjectManager *objectManager = [RKObjectManager sharedManager]; 

    [objectManager getObjectsAtPath:@"/info" 
         parameters:nil 
          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 

           NSArray *info = [mappingResult array]; 
           NSLog(@"Loaded info: %@", info);         
           _info = info; 


          } failure:^(RKObjectRequestOperation *operation, NSError *error) { 

           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting into" 
                       message:[error localizedDescription] 
                       delegate:nil 
                     cancelButtonTitle:@"OK" 
                     otherButtonTitles:nil]; 
           [alert show]; 
           NSLog(@"Hit error: %@", error); 

          }]; 
} 


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    //Initialise RestKit 
    NSURL *URL = [NSURL URLWithString:@"https://myapp.dev"]; 
    AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:URL]; 

    //Enable Activity Indicator Spinner 
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; 


    [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON]; 

    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; 


    RKObjectMapping *eventMapping = [RKObjectMapping mappingForClass:[Info class]]; 

    [infoMapping addAttributeMappingsFromDictionary:@{ 
    @"sample":@"sample", 
    @"sample_1":@"sample_1" 
    }]; 

    RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping 
                         pathPattern:@"/info/:id" 
                         keyPath:nil 
                         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

    [objectManager addResponseDescriptor:infoDescriptor]; 
} 
+0

這適用於單個對象。但是,說你的休息api(在這種情況下來自鐵路)返回多個對象。你如何將它們全部映射到一個對象中? – samdunne

+0

你的迴應是什麼? –

+0

儘管它有內容,它仍然是(null) – samdunne

-1
RKResponseDescriptor *infoDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:infoMapping 
                        pathPattern:nil 
                        keyPath:nil 
                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
+0

你可以解釋你的答案,也可以幫助別人。謝謝。 – Bazinga

+0

我也試過這個,它沒有爲我工作 – samdunne

1

當裝載不具有嵌套的keyPath的對象表示,RestKit存儲字典中的[NSNull null]項下所映射的對象(因爲nil不是有效的詞典鍵)。您可以通過調用,arraydictionary對象RKMappingResult來檢索映射結果,以訪問映射對象。

我看到關於將數組映射到單個對象的後續問題......您的JSON是什麼樣子的?您如何試圖表示它?

+0

https://gist.github.com/4462594 我刪除了一些信息,由於敏感。但我試圖將一個對象與每個JSON條目映射爲一個對象,並通過它們的ID來組織它們 – samdunne