Problem with RKMappingResult. Unable to find a way to get NSDictionary from result

I am trying to include RestKit v.20 in my iOS app. I get the correct conversion result from RKObjectRequestOperation, but I cannot find a good way to put it in an NSDictionary. I am returning an array of hexadecimal value objects. I can get the values ​​of all the specific elements that I want using objectAtIndex: but I need a place that has all the values ​​in its string format, and not in hex. I can only go from hexadecimal to string by setting the corresponding class to a single array object. I need a generic list, so I can predicate it for use in search.

Here are 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 } 

This is where I have problems with the code:

 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 that I can get individual elements, but I need to access the dictionary as a whole, for use in the search.

+4
source share
1 answer

You have a custom CarsInShop class, I think it inherits from NSObject . You do not override the description method to get a default description when registering an instance. By default, the class name and its pointer are used. That is why you get "CarsInShop: 0xa26b0f0" . It does not say anything about the contents of the instance.

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


You can get an array of cars by following these steps:

 NSArray *cars = [carsInShopArray valueForKey:@"car"]; 
0
source

Source: https://habr.com/ru/post/1491997/


All Articles