NSDictionary获得价值

时间:2013-06-07 12:53:49

标签: objective-c ios4 nsdictionary

我在NSDictionary中有这个回应

Venue {
    canonicalUrl = "https://foursquare.com/v/aeroporto-de-congonhas-cgh/4b2d0e1af964a52037cd24e3";
    categories = ({
            icon = {
                prefix = "https://foursquare.com/img/categories_v2/travel/airport_";
                suffix = ".png";
            };
            id = 4bf58dd8d48988d1ed931735;
            name = Aeroporto;
            pluralName = Aeroportos;
            primary = 1;
            shortName = Aeroporto;
        });
    contact = {
        formattedPhone = "+55 11 5090-9000";
        phone = "+551150909000";
        twitter = infraerobrasil;
    };
    hereNow = {
        count = 115;
        groups = ({
                count = 115;
                items = ();
                name = "Outras pessoas aqui";
                type = others;
            });
    };
    id = 4b2d0e1af964a52037cd24e3;
    likes = {
        count = 0;
        groups = ();
    };
    location = {
        address = "Av. Washington Luiz, S/N";
        cc = BR;
        city = "S\U00e3o Paulo";
        country = Brazil;
        crossStreet = "Av. dos Bandeirantes";
        distance = 2355;
        lat = "-23.62596730840704";
        lng = "-46.65872097015381";
        postalCode = "04626-911";
        state = SP;
    };
    name = "Aeroporto de Congonhas (CGH)";
    referralId = "v-1370609443";
    restricted = 1;
    specials = {
        count = 0;
        items = ();
    };
    stats = {
        checkinsCount = 556928;
        tipCount = 1721;
        usersCount = 126970;
    };
    url = "http://www.infraero.gov.br";
    verified = 0;
}

我想从类别中获取pluralName ....

我的代码是:

      -(NSArray*)convertToObjects:(NSArray*)venues{
    NSMutableArray *objects = [NSMutableArray arrayWithCapacity:venues];
    for (NSDictionary *v  in venues) {
        NSLog(@"Venue %@", v);
        NSLog(@"Categoria %@", v[@"categories"][@"pluralName"]); //only here I'm getting error...without this line, name, venue id, address and distance are right
        FSVenue *ann = [[FSVenue alloc]init];
        ann.name = v[@"name"];
        ann.venueId = v[@"id"];
        ann.location.address = v[@"location"][@"address"];
        ann.location.distance = v[@"location"][@"distance"];
        [ann.location setCoordinate:CLLocationCoordinate2DMake([v[@"location"][@"lat"] doubleValue],
                                                      [v[@"location"][@"lng"] doubleValue])];
        [objects addObject:ann];
    }
    return objects;
}

但是我收到了这个错误:

  

- [__ NSCFArray objectForKeyedSubscript:]:无法识别的选择器发送到实例0x1f3e3100 2013-06-07 09:54:51.641 Moodpin [13660:907] *   由于未捕获的异常而终止应用程序   'NSInvalidArgumentException',原因:' - [__ NSCFArray   objectForKeyedSubscript:]:无法识别的选择器发送到实例   0x1f3e3100'   * 第一次抛出调用堆栈:(0x331172a3 0x3ae3f97f 0x3311ae07 0x33119531 0x33070f68 0xb3cfb 0xe9d9f 0xb8eab 0xba681 0x33a526fd   0x339921f9 0x33992115 0x32df445f 0x32df3b43 0x32e1bfcb 0x3305d74d   0x32e1c42b 0x32d8003d 0x330ec683 0x330ebee9 0x330eacb7 0x3305debd   0x3305dd49 0x36c362eb 0x34f73301 0xd9147 0x3b276b20)libc ++ abi.dylib:   终止调用抛出异常

1 个答案:

答案 0 :(得分:3)

看起来categories是一个包含一个元素的数组。您不能在[@"pluralName"]上使用带有字符串NSArray的下标 - 它仅适用于字典。

试试这个 - 获取categories,并选择其初始元素。然后将[@"pluralName"]应用于该元素,如下所示:

NSLog(@"Categoria %@", v[@"categories"][0][@"pluralName"]);
//                                     ^^^