2013-03-23 24 views
1

我一直在嘗試從JSON響應映射下面的對象,並從控制檯輸出中看到的所有內容中,沒有任何理由爲什麼映射不是「T成功 - 我很感激,如果任何人都可以有一個檢查,看看:RestKit - 不是鍵值編碼兼容,在JSON中沒有根

@interface RKElectionsModel : NSObject 

@property (nonatomic, assign) bool isActive; 
@property (nonatomic, strong) NSNumber *electionID; 
@property (nonatomic, strong) NSString *title; 
@property (nonatomic, strong) NSString *summary; 
@property (nonatomic, strong) NSNumber *availableSeats; 
@property (nonatomic, strong) NSNumber *candidatesCount; 
@property (nonatomic, strong) NSNumber *withdrawnCount; 
@property (nonatomic, strong) NSSet *candidates; 

@end 

/** 
* Election Detail Mapping: Getting all election details, we have some extra information from 
* the API call 
* 
*/ 
RKObjectMapping *electionDetailsMapping = [RKObjectMapping mappingForClass:[RKElectionsModel class]]; 

// Map JSON -> entities 
[electionDetailsMapping addAttributeMappingsFromDictionary:@{ 
    @"id": @"electionID", 
    @"title": @"title", 
    @"summary": @"summary", 
    @"active": @"isActive", 
    @"availableSeats": @"availableSeats", 
    @"candidatesCount": @"candidatesCount", 
    @"withdrawnCount": @"withdrawnCount" 
}]; 

// Register our mappings with the provider 
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:electionDetailsMapping pathPattern:@"/api/elections/:electionID/all" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

Console output上引擎收錄和JSON響應的任何實例,您可以訪問here

得到任何幫助, 劉易斯

回答

0

看起來像你試圖將NSString映射到NSNumber。

在您的NSObject嘗試添加以下驗證並將映射啓動時從服務器接收的字符串轉換爲NSNumber。

- (BOOL)validateAvailableSeats:(id *)ioValue error:(NSError **)outError { 
     // Force the value to be a NSNumber 
     *ioValue = [NSNumber numberWithDouble:[(NSString*)value doubleValue] ] 
     return YES; 
} 

RestKit Wiki你會發現你的價值更多的轉換。

相關問題