欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-11-05 11:47:24
ios reason: '-[__nsarraym objectforkeyedsubscript:]: unrecognized selector sent to instance 0x6...

ios reason: '-[__nsarraym objectforkeyedsubscript:]: unrecognized selector sent to instance 0x60800。

一、源代码

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.

}