2013-03-23 57 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你会发现你的价值更多的转换。

相关问题