4

我正在尝试将 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

所以你看我可以获得单个项目,但我需要访问整个字典,以用于搜索。

4

1 回答 1

0

你有一个自定义类CarsInShop,我猜它继承自NSObject. 您不会覆盖该description方法,因此您在记录实例时会获得默认描述。默认值为类名及其指针。这就是为什么你得到"CarsInShop: 0xa26b0f0". 它没有说明实例的内容。

当你说:"I need to access the dictionary as a whole"时,你没有字典。您有一组自定义对象。您仍然可以使用 KVC 搜索数组中的项目(根据日志语句的外观基于键路径)。


您可以通过以下方式获得一系列汽车:

NSArray *cars = [carsInShopArray valueForKey:@"car"];
于 2013-07-18T10:05:52.580 回答