1

我一直在尝试从 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)];

pastebin 上的控制台输出以及您可以在此处访问的任何 JSON 响应示例

感谢任何帮助,刘易斯

4

1 回答 1

0

Seems like your trying to map a NSString to NSNumber.

In your NSObject try to add the following validation and transform the string you receive from the server to a NSNumber when the mapping starts.

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

In the RestKit Wiki you'll find more transformations for your values.

于 2013-03-27T10:59:01.853 回答