简体   繁体   English

如何从嵌套数组/字典iOS 7获取值

[英]How to get values from nested array/ dictionary iOS 7

I fetch values from dictionary and need to display in UITableView, but everything works fine. 我从字典中获取值,需要在UITableView中显示,但是一切正常。

On some spot it stops running and shows thread 在某些地方它停止运行并显示线程

-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0xbfa7670

The code below, which I used to fetch value.. 下面的代码,我用来获取值。

[NSString stringWithFormat:@"%@",[[pageCat1 valueForKeyPath:@"img3"] objectAtIndex:indexPath.row]]

My values are fetched properly in dictionary but lags to display it? 我的值在字典中正确获取,但是显示滞后吗?

pageCat (
    {
    img3 = "http://xxx.in/images/page_cat_img/75x75/4.jpg";
    name = "PVC Flexible Wires";
    page =         (
                    {
            id =                 {
                text = 1;
            };
            img4 = "http://xxxx.in/images/page_img/75x75/1.jpg";
            name = "SINGLE CORE FLEXIBLE WIRES ABOVE 6 SQMM";
        },
                    {
            id =                 {
                text = 72;
            };
            img4 = "http://xxx.in/images/page_img/75x75/72.jpg";
            name = "SINGLE CORE FLEXIBLE WIRES BELOW 6 SQMM";
        }
    );
},
    {
    img3 = "http://xxx.in/images/page_cat_img/75x75/3.jpg";
    name = "Bare Copper Wires";
    page =         {
        id =             {
            text = 29;
        };
        img4 = "http://xxx.in/images/page_img/75x75/29.jpg";
        name = "Tinned Copper Fuse Wires";
    };
},
    {
    img3 = "http://xxx.in/images/page_cat_img/75x75/48.jpg";
    name = "Properties of Wire";
    page =         {
        id =             {
            text = 85;
        };
        img4 = "http://xxx.in/images/page_img/75x75/85.jpg";
        name = "Wires - Normal, HR - PVC, FR, FRLS & Zero Halogen";
    };
}

)

Actually look at the log value, it has array and set of values.. i can't find whether it is in what form.. 实际上看日志值,它具有数组和值集。我找不到它的形式。

Can anyone help me to find the solution?? 谁能帮我找到解决方案?

Thanks, Yazh 谢谢,Yazh

it looks like [pageCat1 valueForKeyPath:@"img3"] returns a NSString and not a NSArray like you expect 它看起来像[pageCat1 valueForKeyPath:@"img3"]返回NSString而不是您期望的NSArray

make sure that it returns a NSArray before applying objectAtIndex : 确保在应用objectAtIndex之前返回NSArray

it seems that pageCat1 is a NSArray so you need to write something like: 看来pageCat1是一个NSArray,因此您需要编写如下内容:

NSString *path = pageCat1[0][@"img3"];

...

As the error already tells, [pageCat1 valueForKeyPath:@"img3"] returns a NSString and you are calling objectAtIndex: on it which is not recognized for this class. 正如错误已经表明的那样, [pageCat1 valueForKeyPath:@"img3"]返回一个NSString并且您正在对其调用objectAtIndex:而该类对此类不识别。 Obviously, pageCat1 differs from what you expected. 显然, pageCat1与您期望的有所不同。

Try NSLog(@"%@", pageCat1); 尝试NSLog(@"%@", pageCat1); to see what it really looks like. 看看它到底是什么样子。

// Edit //编辑

pageCat1 (as seen in your update) is an NSArray that contains items of type NSDictionary . pageCat1 (如更新所示)是一个NSArray ,其中包含NSDictionary类型的项目。 What you really want to do is NSString *imgURL = [[pageCat1 objectAtIndex:indexPath.row] objectForKey:@"img3"]; 您真正想要做的是NSString *imgURL = [[pageCat1 objectAtIndex:indexPath.row] objectForKey:@"img3"];

Explanation: 1. [pageCat1 objectAtIndex:indexPath.row] returns a NSDictionary 2. [__dictionary__ objectForKey:@"img3"] returns the NSString containing your image URL 说明:1. [pageCat1 objectAtIndex:indexPath.row]返回一个NSDictionary 2. [__dictionary__ objectForKey:@"img3"]返回包含您的图像URL的NSString

Actually I used XML data, for that i used third party to parse data. 实际上,我使用XML数据,因为我使用了第三方来解析数据。 Its all of third party which parsed alternate data as array and other as non-array. 它所有将替代数据解析为数组而将其他数据解析为非数组的第三方。 Finally I check the array with 最后我用检查数组

isKindOfClass

and convert it into array. 并将其转换为数组。 Therefore my problem in app solved. 因此,我在应用程序中的问题解决了。 :-) :-)

Thanks to all who help me.. 感谢所有帮助我的人。

Please try this one: 请试试这个:

//Assuming json is your main dictionary    

NSDictionary *pageCat = [json objectForKey:@"pageCat"];
    NSMutableArray *array = [[pageCat valueForKey:@"img3"]mutableCopy];
    NSLog(@"Value=%@", [array objectAtIndex:indexPath.row]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM