我正在尝试将RestKit v.20合并到我的iOS App中。我从RKObjectRequestOperation获得了正确的映射结果,但我无法找到将它放入NSDictionary的好方法。我返回了十六进制值的对象数组。我可以通过使用objectAtIndex获取我想要的所有特定项的值:但是我需要一个具有字符串格式的所有值的地方,而不是十六进制。我只能通过将关联类设置为单个数组对象来从十六进制更改为字符串。我想要一个通用列表,所以我可以预测它用于搜索。
这是JSON数据:
{
"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
}
以下是我遇到代码问题的地方:
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);
}];
这是我从NSLog获得的:
CarsInShopArray: (
"CarsInShop: 0xa26b0f0",
"CarsInShop: 0xa46b2f0",
"CarsInShop: 0xa46af20",
"CarsInShop: 0xa46b7a0",
"CarsInShop: 0xa46bd90",
)
Loaded Car ID #1 -> Name: Mercedes, Year: 2000
所以你看我可以获得单独的项目,但我需要整体访问字典,以便用于搜索。
答案 0 :(得分:0)
您有自定义类CarsInShop
,我猜它继承自NSObject
。您不会覆盖description
方法,因此您在记录实例时会获得默认描述。默认值是类名及其指针。这就是你得到"CarsInShop: 0xa26b0f0"
的原因。它没有说明实例的内容。
当您说:"I need to access the dictionary as a whole"
时,您没有字典。您有一组自定义对象。您仍然可以使用KVC搜索数组中的项目(基于日志语句外观的键路径)。
您可以通过以下方式获得一系列汽车:
NSArray *cars = [carsInShopArray valueForKey:@"car"];