我有一个 API,位于此网址:https://evening-everglades-1560.herokuapp.com/api/v1/stocks/
我正在尝试像这样实现iOS RestKit(这是在MasterViewController.m中):
RKObjectMapping *stockMapping = [RKObjectMapping mappingForClass:[Stock class]];
[stockMapping addAttributeMappingsFromDictionary:@{@"stockId": @"id"}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:stockMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://evening-everglades-1560.herokuapp.com/api/v1/stocks.json"]];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
Stock *stock = [result firstObject];
NSLog(@"mapped w stock: %@", stock);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"failed w error: %@", [error localizedDescription]);
}];
[operation start];
这是记录的内容:
2016-01-17 16:42:08.233 Stocks[9415:1208281] W restkit.object_mapping:RKMapperOperation.m:99 Adding mapping error: No mappable values found for any of the attributes or relationship mappings
2016-01-17 16:42:08.234 Stocks[9415:1208308] I restkit.network:RKObjectRequestOperation.m:250 GET 'https://evening-everglades-1560.herokuapp.com/api/v1/stocks.json' (200 OK / 0 objects) [request=0.4798s mapping=0.0042s total=0.5528s]
2016-01-17 16:42:08.235 Stocks[9415:1208245] mapped w stock: (null)`
我不确定要为
pathPattern
和 keyPath
放什么。
由于 wain 询问“您尚未给出对象定义”,该问题已得到修复。 我忘记在我的
stock.h
可可类中定义提交者。
将
@property(nonatomic, strong) NSString *submitter;
添加到 stock.h 修复了该问题。