显示嵌套的NSMutableArrays和NSDictionarys中的数据

时间:2014-03-15 15:28:23

标签: objective-c cocoa-touch nsarray nsdictionary

我在一个plist中有一个充满字典的数组:

字典本身包含一个数组,由Taskitler键入。我想在表格视图中显示该数组中的项目。我该怎么写才能看到它们?如何定义数据路径?

此代码无效:

cell.textLabel.text = [[mainList objectAtIndex:indexPath.row] objectForKey:@"Urun"]; 

2 个答案:

答案 0 :(得分:1)

您使用NSArray遍历objectAtIndex中的项目,并使用NSDictionary遍历或搜索objectForKey中的对象:

NSArray *root = // ... root object
NSDictionary *dict = [root objectAtIndex:0]
NSArray *task = [dict objectForKey:@"Tasksitler"];
NSDictionary *taskdict = [task objectAtIndex:0];
NSString *urun = [taskdict objectForKey:@"Urun"];

答案 1 :(得分:0)

最简单的方法就是这样做。下面的代码利用了Objective-C中的下标语法。

NSArray *root = // root object
NSString *urun = root[0][@"Tasksitler"][0][@"Urun"];

说明:

myArray[index]相当于[myArray objectAtIndex:index]myDictionary[@"key"]相当于[myDictionary objectForKey:@"key"]

您可以看到哪个更短,更容易理解。

相关问题