简体   繁体   English

如何访问NSArray中的某个值?

[英]How to access a certain value in an NSArray?

I have an array called someArray . 我有一个名为someArray的数组。 I would like to access the name value of the NSArray . 我想访问NSArray的名称值。 I'm trying to access it using the following, but with out any luck. 我正在尝试使用以下方法访问它,但是没有运气。 How do I do it properly? 如何正确执行?

cell.textLabel.text = [[someArray objectAtIndex:indexPath.row] objectForKey:@"name"];

some array {
    haserror = 0;
    headers =     {
        code = 0;
        haserror = 0;
        nodeid = "fe1.aaaaaa.2.undefined";
        time = 16;
    };
    results =     (
                {
            coords =             (
                "44.916667",
                "8.616667"
            );
            id = 2;
            key = alessandria;
            name = Alessandria;
            state = Piemonte;
            zip = 1512;
        },
                {
            coords =             (
                "43.616944",
                "13.516667"
            );
            id = 3;
            key = ancona;
            name = Ancona;
            state = Marche;
            zip = 601;
        },

}

As far as i see from you data model, the key name is a node under the key results . 据我从您的数据模型看,键name是键results下的一个节点。 You can use this data model as a dictionary map, the code snippet below must give you what you need.. 您可以将此数据模型用作字典映射,下面的代码段必须为您提供所需的信息。

NSDictionary *myObject = [[someArray objectAtIndex:indexPath.row] objectForKey:@"results"];
cell.textLabel.text = [myObject objectForKey:"name"];

IMPORTANT NOTE: If you have some lopps or some other mechanisms for receiving data, there may be more efficent ways for your sıolution, so please give some more additional info about what you are exactly tryin to do 重要说明: 如果您有一些便携式计算机或其他一些用于接收数据的机制,则可能有更有效的解决方法,因此请提供更多有关尝试使用的信息

As others have noted, someArray is a dictionary while results is a key pointing to an array inside of your dictionary. 正如其他人指出的那样, someArray是一个字典,而results是一个指向字典内部数组的键。 If you want an array of all of the name fields in your results array, you could use valueForKeyPath: on the someArray variable, like this: 如果您希望您的所有在名称字段的数组results数组,你可以使用valueForKeyPath:someArray变量,就像这样:

NSArray *names = [someArray valueForKeyPath:@"results.name"];

The names variable should now contain "Alessandria", and "Ancona" from the data set your show in your example code. 现在, names变量应包含示例代码中显示的数据集中的“亚历山德里亚”和“安科纳”。

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

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