iosobjective-crestrestkit

Objective-C RestKit no Output


I am using RestKIT to make a GET Request for an API that I want to use. Sadly there is no output. This is the output of the GET Request:

{
  "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": ""
      }
    ]
  }
}

Obviously I just want an array, where all the products are stored. This is my method to do that.

- (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];

}

I made two classes, one for the result that has a NSNumber "products_count" and one Product class with all the product attitudes. The problem is that even though the connection is made and there is no "error", mappingResult.array is apparently empty. What is the mistake in the code?


Solution

  • You don't actually need your Result class as the array will already give you the account and you can go direct to the array.

    So, your initial problem is that you're using the wrong mapping in the response descriptor. You just need to switch to the result mapping.

    You can use the product mapping in the response descriptor, but you should change the key path to result.products.

    Your product mapping also has an issue because description is an existing method on all NSObject subclasses so you need to change your usage of it by adding a property called something like overview or title.