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

时间:2014-06-05 10:57:22

标签: ios multithreading uitableview dictionary

我从字典中获取值并需要在UITableView中显示,但一切正常。

在某些地方,它停止运行并显示线程

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

下面的代码,我用来获取值..

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

我的值在字典中正确获取但是滞后显示它?

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";
    };
}

实际看一下日志值,它有数组和一组值..我无法找到它是以什么形式...

任何人都可以帮我找到解决方案吗?

谢谢, Yazh

4 个答案:

答案 0 :(得分:2)

看起来[pageCat1 valueForKeyPath:@"img3"]会像您期望的那样返回NSString而不是NSArray

确保在应用NSArray之前返回objectAtIndex

似乎pageCat1是NSArray所以你需要编写类似的东西:

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

...

答案 1 :(得分:1)

正如错误已经说明的那样,[pageCat1 valueForKeyPath:@"img3"]会返回NSString并且您正在调用objectAtIndex:,而此类无法识别该pageCat1。显然,NSLog(@"%@", pageCat1);与您的预期不同。

尝试pageCat1查看其真实情况。

//编辑

NSArray(如您的更新中所示)是NSDictionary,其中包含NSString *imgURL = [[pageCat1 objectAtIndex:indexPath.row] objectForKey:@"img3"];类型的项目。你真正想做的是[pageCat1 objectAtIndex:indexPath.row]

说明: 1. NSDictionary返回[__dictionary__ objectForKey:@"img3"] 2. {{1}}返回包含图片网址的NSString

答案 2 :(得分:1)

实际上我使用的是XML数据,因为我用第三方来解析数据。它的所有第三方都将备用数据解析为数组,将其他数据解析为非数组。最后我用

检查数组
isKindOfClass

并将其转换为数组。因此我在app中的问题解决了。 : - )

感谢所有帮助过我的人..

答案 3 :(得分:0)

请尝试这个:

//Assuming json is your main dictionary    

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