iOS json 数据遇到[__NSCFString count] 错误

今天开发遇到这样的问题

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x60000024ba30'

错误是这样的,我用一个数组来接收后台的json,而且是NSArray,但是使用for循环出错了

 NSArray * dropArr = [_subBattelDicobjectForKey:@"thingsDrop"];

    

    for (int i =0; i < dropArr.count; i++) {

        NSDictionary * dropDic = dropArr[i];

        NSString * typeStr = [dropDicobjectForKey:@"type"];

        UILabel * thingDropLb = [XDUIViewlabelWithFrame:CGRectMake(20,60 + 24 * i,300, 18)text:typeStr textColor:[UIColorgreenColor] fontSize:14];

        [self.firstBackViewaddSubview:thingDropLb];

    }


}

找了半天,不知哪错了

最后发现 [[_subBattelDic objectForKey:@"thingsDrop"] class]类是一个NSString

为什么会这样呢?

虽然看上去是个数组,但有\存在

解决办法:

NSString * dropStr = [_subBattelDicobjectForKey:@"thingsDrop"];

    

NSData * data = [dropStr dataUsingEncoding:NSUTF8StringEncoding];

NSArray * dropArr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


将它反序列化,成功!

你可能感兴趣的:(iOS json 数据遇到[__NSCFString count] 错误)