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

Posted

技术标签:

【中文标题】由于未捕获的异常\'NSInvalidArgumentException\'而终止应用程序,原因:\'-[__NSCFArray objectForKey:]:无法识别的选择器发送到实例【英文标题】:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFArray objectForKey:]:无法识别的选择器发送到实例 【发布时间】:2016-01-09 22:25:59 【问题描述】:

我正在尝试解析 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"]);

    

每当我尝试解析照片字典以获取“photo_reference”时,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;
    
)

谁能告诉我我的问题是什么。注意:照片字典是包含“vicinity”、“icon”、“lat”、“lng”的更大字典的一部分

【问题讨论】:

【参考方案1】:

问题是你得到了一个数组(NSArray),但你试图输入它并将其视为字典(NSDictionary)。您所说的“照片字典”不是,实际上是字典。正如您自己的NSLog(@"photo %@", photo) 结果显示的那样,它是一个数组(在这种情况下,包含一个元素,它一个字典)。

【讨论】:

以上是关于由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFArray objectForKey:]:无法识别的选择器发送到实例的主要内容,如果未能解决你的问题,请参考以下文章

由于未捕获的异常而终止应用程序 [UIImageView _isResizable]

由于未捕获的异常而终止应用程序,同时加载视图

由于未捕获的异常“NSInternalInconsistencyException”错误而终止应用程序

“由于未捕获的异常而终止应用程序”在推送视图控制器时崩溃

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序