我有这个JSON(一个字典数组):
[
{"id":"BTCLTC","last":"89.767","high":"96.185","low":"25.000","bid":"89.729","ask":"91.320","volume":"29.78918","scale":3},
{"id":"BTCUSD","last":"443.799","high":"444.092","low":"394.570","bid":"439.110","ask":"446.760","volume":"4.68266","scale":3},
{"id":"BTCXRP","last":"98101.500","high":"98101.500","low":"86000.000","bid":"94999.050","ask":"97499.900","volume":"21.66779","scale":3}
]
我想使用RKObjectMappingMatcher
来匹配"id":"BTCUSD"
。
修改
感谢Wain的评论,使用RKObjectMapping
足以让KVC验证起作用。验证方法由RestKit自动调用。
到目前为止我的代码看起来像:
RKObjectMapping *defaultMapping = [RKObjectMapping mappingForClass:class];
NSDictionary *attributeMappings = [query objectForKey:@"attributeMappings"];
[defaultMapping addAttributeMappingsFromDictionary:attributeMappings];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping method:RKRequestMethodFromString(@"GET") pathPattern:@"/api/v1/markets" keyPath:@"" statusCodes:[NSIndexSet indexSetWithIndex:RKStatusCodeClassSuccessful]];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
[manager addResponseDescriptor:responseDescriptor];
我添加了我认为对NSObject
进行验证所必需的内容。
- (BOOL)validateCurrencyConversionID:(id *)ioValue error:(NSError **)outError
{
if ([(NSString*)*ioValue length] == 0)
{
*outError = [NSError errorWithDomain:RKErrorDomain code:100 userInfo:@{@"description":@"Empty string"}];
return NO;
}
else if ([((NSString*)*ioValue) rangeOfString:@"USD"].location == NSNotFound)
{
*outError = [NSError errorWithDomain:RKErrorDomain code:101 userInfo:@{@"description":@"Doesn't contain USD"}];
return NO;
}
return YES;
}
答案 0 :(得分:0)
不要使用这种方法,因为你不能适当地索引数组,因此你需要处理一个你期望字符串的数组。
相反,允许为每个映射运行映射,然后使用KVC validation中止您不想要的项目的映射。