我正在尝试将 RestKit v.20 合并到我的 iOS 应用程序中。我从 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
所以你看我可以获得单个项目,但我需要访问整个字典,以用于搜索。