【发布时间】:2016-01-17 22:33:54
【问题描述】:
我在这个 url 上有一个 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 放什么。
提前致谢!
【问题讨论】:
-
只是看起来映射是错误的方式:
@"id": @"stockId" -
当我切换它们时,我得到这个错误:
valueForUndefinedKey:]: this class is not key value coding-compliant for the key stockId. -
那把钥匙是从哪里来的?我下载的 JSON 中没有看到,你也没有给出你的对象定义
-
我只是想让一些东西与 Restkit 和我的 API 一起工作。我不确定如何用这个配置 Restkit。 this API 的任何工作示例都会非常有用。
标签: ios django api django-rest-framework restkit