天天看點

iOS reason: '-[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0x60800

//聯系人:石虎  QQ: 1224614774昵稱:嗡嘛呢叭咪哄

一、源代碼

NSDictionary * dic  = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

NSLog(@"dic === %@",dic);

列印結果:dic === {

       len = 69;

       list = (

       {         

                //是字段資料   

                }

for (NSDictionary * subDic in dic[@"list"]) {

    NSLog(@“周遊資料”);

}

如果背景資料格式修改不是dic[@"list”]),就是崩潰報錯如下:

reason: '-[__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance 0x60800025e840'

二、解決方案

解決方法一:

NSMutableArray *topLevelArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

NSDictionary *dict = topLevelArray[0];

for (NSDictionary * subDic in dict) {

    NSLog(@“周遊資料”);

}

解決方法二:

如果您想要檢查你的什麼,你可以使用 isKindOfClass:像這樣:

id jso = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

if (jso == nil) {

        // Error.  You should probably have passed an NSError ** as the error

        // argument so you could log it.

} else if ([jso isKindOfClass:[NSArray class]]) {

        NSArray *array = jso;

        // process array elements

} else if ([jso isKindOfClass:[NSDictionary class]]) {

        NSDictionary *dict = jso;

        // process dictionary elements

} else {

        // Shouldn't happen unless you use the NSJSONReadingAllowFragments flag.

}

謝謝!!!