Objective-C RestKit 无输出

问题描述 投票:0回答:1

我正在使用 RestKIT 对我想要使用的 API 发出 GET 请求。遗憾的是没有输出。这是 GET 请求的输出:

{
  "return_code": 0,
  "return_message": "",
  "result": {
    "products_count": 2,
    "product": [
      {
        "id": "2440508995",
        "name": "Long Sweater",
        "description": "This product guarantees warmth in times of need.",
        "price": 8,
        "categories_ids": "73978179"
      },
      {
        "id": "2455113539",
        "name": "Tommy Hilfiger Pants",
        "description": "Exclusive brown pants",
        "price": 13,
        "categories_ids": ""
      }
    ]
  }
}

显然我只想要一个数组,其中存储所有产品。这是我的方法。

- (void)loadProducts
{
    RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[Result class]];
    [resultMapping addAttributeMappingsFromDictionary:@{
                                                          @"products_count": @"products_count",

                                                          }];

    RKObjectMapping* productMapping = [RKObjectMapping mappingForClass:[Product class]];
    [productMapping addAttributeMappingsFromDictionary:@{
                                                         @"id": @"id",
                                                         @"name": @"name",
                                                         @"description": @"description",
                                                         @"price": @"price",
                                                         @"categories_ids": @"categories_ids"
                                                         }];

    [resultMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"product" toKeyPath:@"product" withMapping:productMapping]];



    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"result" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    NSURL *URL = [NSURL URLWithString:@"https://api.api2cart.com/v1.0/product.list.json?api_key=//some keyf&store_key=//some key"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
    [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        RKLogInfo(@"Load collection of Products: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Operation failed with error: %@", error);
    }];

    [objectRequestOperation start];

}

我创建了两个类,一个用于具有 NSNumber“products_count”的结果,一个用于包含所有产品态度的产品类。问题是,即使建立了连接并且没有“错误”,mappingResult.array 显然是空的。代码有什么错误?

ios objective-c rest restkit
1个回答
1
投票

您实际上并不需要您的

Result
类,因为数组已经为您提供了帐户,您可以直接进入数组。

因此,您最初的问题是您在响应描述符中使用了错误的映射。您只需切换到结果映射即可。

您可以在响应描述符中使用产品映射,但您应该将关键路径更改为

result.products

您的产品映射也存在问题,因为

description
是所有
NSObject
子类上的现有方法,因此您需要通过添加名为
overview
title
之类的属性来更改它的用法。

© www.soinside.com 2019 - 2024. All rights reserved.