由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSCFArray objectForKey:]:发送到实例的无法识别的选择器

时间:2016-01-09 22:26:00

标签: ios objective-c json dictionary

我正在尝试解析Google Map Web Service JSON,这是我的代码。数据是从JSON解析的数组

- (void)plotPositions:(NSMutableArray *)data
{
    //Loop through the array of places returned from the Google API.
    for (int i=0; i<[data count]; i++)
    {
    //Retrieve the NSDictionary object in each index of the array.
    NSDictionary *place = [data objectAtIndex:i];

    //retrieve photo preference number
    NSDictionary *photo = [place objectForKey:@"photos"];
    NSLog(@"photo %@", photo);

    //There is a specific NSDictionary object that gives us location info.
    NSDictionary *geo = [place objectForKey:@"geometry"];

    //Get our name and address info for adding to a pin.
    NSString *name=[place objectForKey:@"name"];
    NSString *vicinity=[place objectForKey:@"vicinity"];
    NSLog(name, vicinity);
    [displayName addObject:name];
    [searchLocation addObject:vicinity];
    [ThumbnilURL addObject:[place objectForKey:@"icon"]];

    //Get the lat and long for the location.
    NSDictionary *loc = [geo objectForKey:@"location"];

    //Create a special variable to hold this coordinate info.
    CLLocationCoordinate2D placeCoord;

    //Set the lat and long.
    placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
    placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];

    //get photo preference
    NSLog(@"photo reference %@", [photo objectForKey:@"photo_reference"]);

    }
}

每当我尝试解析照片词典以获得&#34; photo_reference&#34;时,xcode都会出错。这是照片词典:

(
    {
    height = 4896;
    "html_attributions" =         (
        "<a href=\"https://maps.google.com/maps/contrib/114143467670122734398/photos\">Tim Stewart</a>"
    );
    "photo_reference" = "CmRdAAAAiXB3cFbK3GXj0_SJz6x6K01vccCo5Vu7kVBLDIATT6YaPwzfThX7TnmLeTZHL-QYpQYF3MH1aw-3vVoc58-d3FfWUexbLqf6ioScZfAbU9XUh2A7HX-A1BFoZ1hetbcFEhCPVmWQfCFXWPuTkV6Qya6eGhTQ-sn_mkWO8IV3-_2PnD4GTH7w9A";
    width = 3672;
    }
)

有人可以告诉我我的问题是什么。注意:照片词典是一个较大的字典的一部分,其中包含&#34;附近&#34;,&#34;图标&#34;,&#34; lat&#34;,&#34; lng&#34;

1 个答案:

答案 0 :(得分:3)

问题是您正在获得一个数组(NSArray),但您正在尝试键入它并将其视为字典(NSDictionary)。你所谓的&#34;照片词典&#34; ,实际上是一本字典。正如您自己的NSLog(@"photo %@", photo)结果所示,一个数组(在本例中包含一个元素,其中 是一个字典)。