NSMutableArray中的单独对象或字符串?

时间:2016-03-08 10:39:35

标签: ios objective-c json nsjsonserialization

我对JSon的新东西,所以请耐心等待。我从URL反序列化JSon,一切都很好,直到我尝试分离其中的对象。该应用程序崩溃,我得到一个我不明白的错误。也许你可以帮我看看我错过了什么。

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

    if ([data length]>0 && error == nil) {

        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

        if (jsonObject != nil && error == nil) {

            if ([jsonObject isKindOfClass:[NSDictionary class]]) {

                NSDictionary *deserializedDictionary = [[NSDictionary alloc] init];
                deserializedDictionary = jsonObject;
                NSLog(@"Deserialized Dictionary = %@",deserializedDictionary);
                /*
                LOG: Deserialized Dictionary = { d = "[{\"unit\":\"P101\",\"price\":36.0000,\"stat\":\"process\",\"type\":\"P12\"},{\"unit\":\"P102\",\"price\":38.0000,\"stat\":\"process\",\"type\":\"P13\"},..}
                */

                NSMutableArray *dicts = [[NSMutableArray alloc] init];
                dicts = (NSMutableArray *)deserializedDictionary[@"d"];
                NSLog(@"Print dicts: %@",dicts);
                /*
                LOG: Print dicts: [{"unit":"P101","price":36.0000,"stat":"process","type":"P12"},{"unit":"P102","price":38.0000,"stat":"process","type":"P13"},..]
                */

                NSLog(@"%@",NSStringFromClass([dicts class]));
                //LOG: __NSCFString

                NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];

                for (myDict in dicts)
                {
                    NSLog(@"myDict objectForKey: id-> %@ myDict objectForKey: result-> %@",[myDict objectForKey:@"unit"],[myDict objectForKey:@"result"]);
                }
            }

然后我收到了这个错误:

  

[__ NSCFString countByEnumeratingWithState:objects:count:]:无法识别的选择器发送到实例0x7fe97b327790   2016-03-08 11:29:12.946 Poop [49680:5673839] ***由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [__ NSCFString countByEnumeratingWithState:objects:count:] :无法识别的选择器发送到实例0x7fe97b327790'

请帮帮忙?

3 个答案:

答案 0 :(得分:1)

从您的代码和日志中,我可以理解实际问题是在服务器端,从这些行开始

NSMutableArray *dicts = [[NSMutableArray alloc] init];
dicts = (NSMutableArray *)deserializedDictionary[@"d"];
NSLog(@"Print dicts: %@",dicts);
/*
 LOG: Print dicts: [{"unit":"P101","price":36.0000,"stat":"process","type":"P12"},{"unit":"P102","price":38.0000,"stat":"process","type":"P13"},..]
*/

NSLog(@"%@",NSStringFromClass([dicts class]));
//LOG: __NSCFString

Log表示名为 dicts 的变量的类型为 NSCFString 而非 NSMutableArray ,并且NSString没有密钥且枚举无法运行在 NSString 类型的对象上。

问题在于您的API响应,它没有返回正确的JSON。

解决方案是在服务器端更改以返回数组或字典而不是字符串。

答案 1 :(得分:0)

参见编辑过的代码:

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

if ([data length]>0 && error == nil) {

    id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    if (jsonObject != nil && error == nil) {

        if ([jsonObject isKindOfClass:[NSDictionary class]]) {

            NSDictionary *deserializedDictionary = [[NSDictionary alloc] init];
            deserializedDictionary = jsonObject;
            NSLog(@"Deserialized Dictionary = %@",deserializedDictionary);
            /*
            LOG: Deserialized Dictionary = { d = "[{\"unit\":\"P101\",\"price\":36.0000,\"stat\":\"process\",\"type\":\"P12\"},{\"unit\":\"P102\",\"price\":38.0000,\"stat\":\"process\",\"type\":\"P13\"},..}
            */

            NSMutableArray *responseArray = [[NSMutableArray alloc] init];
            responseArray = deserializedDictionary[@"d"];
            NSLog(@"Print responseArray: %@",responseArray);
            /*
            LOG: Print dicts: [{"unit":"P101","price":36.0000,"stat":"process","type":"P12"},{"unit":"P102","price":38.0000,"stat":"process","type":"P13"},..]
            */

            NSLog(@"%@",NSStringFromClass([responseArray class]));
            //LOG: __NSCFString


           //The correct way of fast enumeration.

            for (NSMutableDictionary *myDict in dicts)
            {
                NSLog(@"myDict objectForKey: id-> %@ myDict objectForKey: result-> %@",[myDict objectForKey:@"id"],[myDict objectForKey:@"result"]);
            }

        }

此外,每次从字典中获取值时,都应该始终检查字典中是否存在键,为此可以在HelperClass中添加此方法,

//Check is key exist in the dictionary
+(BOOL)validateKeyValueForData:(id)dataValue {

if([dataValue isEqual:[NSNull null]] || dataValue == nil)
{
    return NO;
}

if([dataValue isKindOfClass:[NSArray class]] || [dataValue isKindOfClass:[NSMutableArray class]])
{
    if([dataValue isEqual:[NSNull null]] || dataValue == nil || [dataValue count] <= 0)
    {
        return NO;
    }
}
else
    if([dataValue isKindOfClass:[NSDictionary class]] || [dataValue isKindOfClass:[NSMutableDictionary class]])
    {
        if([dataValue isEqual:[NSNull null]] || dataValue == nil || [dataValue count] <= 0)
        {
            return NO;
        }
    }
    else if ([dataValue isKindOfClass:[NSString class]] || [dataValue isKindOfClass:[NSMutableString class]])
    {
        if([dataValue isEqual:[NSNull null]] || dataValue == nil || [dataValue length] <= 0)
        {
            return NO;
        }
    }

return YES;
}

并将其用作

for (NSMutableDictionary *myDict in dicts)
{

    //This way you make sure that the value for the specified key is exist in the dictionary. 
    if ([HelperClass validateKeyValueForData:myDict[@"id"]] && [HelperClass validateKeyValueForData:myDict[@"result"]]) {

        NSLog(@"myDict objectForKey: id-> %@ myDict objectForKey: result-> %@",[myDict objectForKey:@"id"],[myDict objectForKey:@"result"]);
   }

}

答案 2 :(得分:0)

看起来dicts对象以某种方式被实例化为字符串。在将原始NSData解析为JSON对象之前,请查看原始NSString *rawResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Raw data: %@", rawResponse);

/tmp

如果设置一个通用断点来捕获所有异常,它可能会停止在您尝试迭代dicts数组的位置。您可以通过以下方式执行此操作:

  1. 选择断点导航器
  2. 点击左下方的“+”按钮
  3. 选择“添加例外断点...”
  4. 在弹出选项菜单中,选择Break'On Catch'选项