4

I am trying to incorporate RestKit v.20 into my iOS App. I get the proper mapping result from RKObjectRequestOperation but I can't figure out a good way to put it into a NSDictionary. I am returned an array of objects of hex values. I can get the values of all the specific items I want by using objectAtIndex: but I need a place that has all the values in its string format, not hex. I can only change from hex to string by setting the associated class equal to a single array object. I want a general list so I can predicate it for use in search.

Here is the JSON Data:

{
    "carsInShop": [
        {
            "car": {
                "id": "1",
                "name": "Mercedes",
                "year": "2000"
            }
        },
        {
            "car": {
                "id": "2",
                "name": "BMW",
                "year": "2004"
            }
        },
        {
            "car": {
                "id": "3",
                "name": "Audi",
                "year": "2001"
            }
        },
        {
            "car": {
                "id": "4",
                "name": "Lexus",
                "year": "2011"
            }
        },
        {
            "car": {
                "id": "5",
                "name": "Toyota",
                "year": "2006"
            }
        },
    ],
    "count": 5
}

Here is where I am having code problems:

NSURL *URL = [NSURL URLWithString: @"http://localhost:8888/testphpread/index.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request
                                                 responseDescriptors:@[responseDescriptor]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    RKLogInfo(@"CarsInShopArray: %@", mappingResult.array);

    carsInShopArray = [mappingResult array];
    CarsInShop* carsInShop = [carsInShopArray objectAtIndex:0];


    NSLog(@"Loaded Car ID #%@ -> Name: %@, Year: %@", carsInShop.car.id, carsInShop.car.name, carsInShop.car.year);

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];

This is what I get from NSLog:

CarsInShopArray: (
    "CarsInShop: 0xa26b0f0",
    "CarsInShop: 0xa46b2f0",
    "CarsInShop: 0xa46af20",
    "CarsInShop: 0xa46b7a0",
    "CarsInShop: 0xa46bd90",
)

Loaded Car ID #1 -> Name: Mercedes, Year: 2000

So you see I can get individual items, but I need to access the dictionary as a whole, for use in searching.

1 Answers1

0

You have a custom class CarsInShop, I guess it inherits from NSObject. You don't override the description method so you get the default description when you log the instance. The default is the class name and its pointer. That's why you get "CarsInShop: 0xa26b0f0". It says nothing about the contents of the instance.

When you say: "I need to access the dictionary as a whole", you don't have a dictionary. You have an array of custom objects. You can still use KVC to search through the items in the array (based on key path by the look of your log statement).


You can get an array of the cars by doing:

NSArray *cars = [carsInShopArray valueForKey:@"car"];
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Is it possible to have the `CarsInShop *carsInShop` object hold the whole array? As of now, I've been using `carsInShop = [carsInShopArray objectAtIndex:0]` or `indexPath.row`. This is only one item though. How can I access `carsInShop.car` and have it show me all the cars the array holds? – Richard McCluskey Jul 18 '13 at 18:23
  • Or is it possible to change the `carsInShopArray` into an array filled with the contents of the instance instead of the pointer? – Richard McCluskey Jul 18 '13 at 19:16
  • Thank you for this answer. After looking at your edit, I found that my predicate had to be changed from `SELF.car.name` to `SELF.%K.%K`. – Richard McCluskey Jul 18 '13 at 21:36