使用 Django Rest Framework 设置 iOS Restkit
Setting up iOS Restkit with Django Rest Framework
我有一个 API 坐在这个 url: 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 询问 "you haven't given your object definition." 我忘记在我的 stock.h
cocoa class 中定义提交者。
将 @property(nonatomic, strong) NSString *submitter;
添加到 stock.h 修复了它。
我有一个 API 坐在这个 url: 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 询问 "you haven't given your object definition." 我忘记在我的 stock.h
cocoa class 中定义提交者。
将 @property(nonatomic, strong) NSString *submitter;
添加到 stock.h 修复了它。