使用Django Rest Framework设置iOS Restkit

时间:2016-01-17 22:33:54

标签: ios django api django-rest-framework restkit

我在这个网址上有一个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)`

我不确定要为pathPatternkeyPath添加什么。

提前致谢!

1 个答案:

答案 0 :(得分:0)

通过wain查询"您还没有给出您的对象定义,这是固定的。"我忘了在我的stock.h可可类中定义提交者。

@property(nonatomic, strong) NSString *submitter;添加到stock.h修复它。