创建一个字典财产清单编程(Create a dictionary property list pro

2019-07-30 04:24发布

我想以编程方式创建要我的UITableView提要数据字典,但我有一个很难用它。 我想创建一个类似于字典的属性列表(图)给予或采取几个项目。

我已经看了“属性列表编程指南:创建属性列表编程”和我想到了我自己的一个小样本:

//keys

NSArray *Childs = [NSArray arrayWithObjects:@"testerbet", nil];
NSArray *Children = [NSArray arrayWithObjects:@"Children", nil];
NSArray *Keys = [NSArray arrayWithObjects:@"Rows", nil];
NSArray *Title = [NSArray arrayWithObjects:@"Title", nil];

//strings

NSString *Titles = @"mmm training";

//dictionary 

NSDictionary *item1 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
NSDictionary *item2 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
NSDictionary *item3 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
NSArray *Rows = [NSArray arrayWithObjects: item1, item2, item3, nil];
NSDictionary *Root = [NSDictionary dictionaryWithObject:Rows forKey:Keys];

// NSDictionary *tempDict = [[NSDictionary alloc] //initWithContentsOfFile:DataPath];
NSDictionary *tempDict = [[NSDictionary alloc] initWithDictionary: Root];

我试图使用层次结构的这一数据为我的表视图。

所以我想知道我怎么能可以创造我的财产名单(字典)编程,这样我可以用我自己的数组中填充它。

我还是新与iPhone发展,容忍我。 ;)

Answer 1:

这是一种情况,“授人以渔”,是一个大大更有帮助的做法不是“授人以鱼”。 一旦你理解了基本原则和NSDictionary中API,它变得更加容易地制定自己的定制解决方案。 这里有几个观察和学习要点:

  • 该方法+dictionaryWithObject:forKey:用于与单个键-值对创建一个NSDictionary。 它不会接受以逗号分隔的参数每个冒号后(:)在方法调用中,只有一个。 要创建具有多个键-值对的字典,使用2种相关的方法之一: +dictionaryWithObjects:forKeys:它接受含有值和键,2个NSArray的物体或+dictionaryWithObjectsAndKeys:其中交替(对象,项,对象,密钥)与终止nil的说法。
  • 简化创建代码。 你不需要一百万局部变量只是为了创造的东西。 使用内联参数在有意义。 一个办法做到这一点的代码来构建你的字典作为一个的NSMutableDictionary ,然后(如有必要)通过调用使得它的一个不可改变的副本-copy就可以了。 (请记住,复制方法返回一个新的对象,您必须释放,以避免内存泄漏。)你不必这样,有一个变量的每一个值,所以你可以做结构的“一点击”创建(S )在每个级别。
  • 使用+arrayWithObject:用一个单一的对象创建一个NSArray。
  • (风格建议)从不使用一个大写字母开始的变量,方法或功能的名称。 (请注意,SO亮点领先帽变量,如类名)。这肯定会帮助其他人谁被迷惑你的意图,你的命名方案,阅读你的代码。

只给你一个什么样的链接的图像创建字典可能看起来像在代码中的味道......(我忽略了你所选择的变量名和同时使用的完整性不同的方法。)

NSDictionary *item1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Screen J",[NSNumber numberWithInt:3],nil]
                                                  forKeys:[NSArray arrayWithObjects:@"Title",@"View",nil]];
NSDictionary *item2 = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"Screen I", @"Title", 
                                        [NSArray arrayWithObject:item1], @"Children", 
                                        nil];
...


Answer 2:

我不知道我理解这里的基本目标。

这似乎是在运行时,你正在构建一个深刻的字典,许多子节点。 但是,你正在构建这一切与静态代码...为什么你能不能简单地做一个plist中(就像一个你有一个图像)和读取到一个NSDictionary? 双方的NSDictionary的NSArray和有让你简单地在一个文件中读取并获得填写对象的整体方法。 然后,它是比较容易的方式编辑和理解。 该方法是dictionaryWithContentsOfFile

如果所有数据前将其放入字典是真正在运行时创建的,那么它看起来像你想的代码非常不同的,递归式的,而不是给出的平的例子。

最后,我个人不喜欢的dictionaryWithObjects:forKeys:方法NSDictionary的建设的字典。 如果你不得不做的事情,这样我非常喜欢的另一种方法dictionaryWithObjectsAndKeys:它做同样的事情,但保持与对象的键:

NSDictionary *item1 = [NSDictionary dictionaryWithObjectsAndKeys:
                       @"Screen J",
                       @"Title",
                       [NSNumber numberWithInt:3],
                       @"View"];


Answer 3:

NSMutableDictionary *topLevel = [NSMutableDictionary dictionary];

NSMutableDictionary *item1 = [NSMutableDictionary dictionary];
NSString *item1title = [NSString stringWithString:@"Title 1"];
NSMutableDictionary *item1children = [NSMutableDictionary dictionary];

//  create children
NSString *item1child1 = [NSString stringWithString:@"item 1, child 1"];

NSMutableDictionary *item1child2 = [NSMutableDictionary dictionary];
NSString *item1child2title = [NSString stringWithString:@"Title 1-2"];
NSMutableDictionary *item1child2children = [NSMutableDictionary dictionary];

NSString *item1child2child1 = [NSString stringWithString:@"item 1, child 2, child 1"];
NSString *item1child2child2 = [NSString stringWithString:@"item 1, child 2, child 2"];

[item1child2 setObject:item1child2title forKey:@"Title"];
[item1child2children setObject:item1child2child1 forKey:@"item 1 child2 child 1"];
[item1child2children setObject:item1child2child2 forKey:@"item 1 child2 child 2"];

[item1child2 setObject:item1child2children forKey:@"children"];

//  add children to dictionary
[item1children setObject:item1child1 forKey:@"item1 child1"];
[item1children setObject:item1child2 forKey:@"item1 child2"];

//  add to item 1 dict
[item1 setObject:item1title forKey:@"Title"];
[item1 setObject:item1children forKey:@"children"];

NSMutableDictionary *item2 = [NSMutableDictionary dictionary];
NSString *item2title        = [NSString stringWithString:@"Title"];
NSMutableDictionary *item2children  = [NSMutableDictionary dictionary]; 

NSString *item2child1 = [NSString stringWithString:@"item 2, child 1"];
NSString *item2child2 = [NSString stringWithString:@"item 2, child 2"];
NSString *item2child3 = [NSString stringWithString:@"item 2, child 3"];

//  add children to dictionary
[item2children setObject:item2child1 forKey:@"item2 child1"];
[item2children setObject:item2child2 forKey:@"item2 child2"];
[item2children setObject:item2child3 forKey:@"item2 child3"];

//  add to item 2 dict
[item2 setObject:item2title forKey:@"Title"];
[item2 setObject:item2children forKey:@"children"];

[topLevel setObject:item1 forKey:@"Item 1"];
[topLevel setObject:item2 forKey:@"Item 2"];


Answer 4:

首先..超强! 感谢..我真的很感激的解释和代码段。 既然你给了我这么好的解释,我希望你不介意我问一对夫妇更多的问题。

首先,我确实如你所说,这就是我想出了:(我用我的原物业列表,而不是本示例的这一时间,所以这是在钻表得到他(或需要得到他)treestructure)。

http://img509.imageshack.us/img509/7523/picture2lsg.png

NSDictionary *root = [NSMutableDictionary dictionary];
NSDictionary *item1 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:@"VirtuaGym Online"] forKey:[NSArray arrayWithObjects:@"Title"]];
NSDictionary *item2 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:@"Do the training"] forKey:[NSArray arrayWithObjects:@"Title"]];
NSDictionary *item3 = ... 

[root setObject:item1 forKey:@"Item 1"];
[root setObject:item2 forKey:@"Item 2"];

也做了一些研究,并试图与其他一些输入别的东西..

NSMutableArray *Rows = [NSMutableArray arrayWithCapacity: 1];

for (int i = 0; i < 4; ++i) {
  NSMutableArray *theChildren = [NSMutableArray arrayWithCapacity: 1];
  [theChildren addObject: [NSString stringWithFormat: @"tester %d", i]];
NSString *aTitle = [NSString stringWithFormat: @"Item %d", i];
  NSDictionary *anItem = [NSDictionary dictionaryWithObjectsAndKeys: aTitle, @"Title", theChildren, @"Children"];
  [Rows addObject: anItem];
}

NSDictionary *Root = [NSDictionary withObject: Rows andKey: @"Rows"];

我决定只是测试这两种然而我想要做什么。 它给了我一个EXC_BAD_ACCESS错误。

我也想知道,因为我看到你在你的代码片段使用数量,不能也使用NSString因为这就是plist中使用..可能是完全在这里,当然

NSDictionary *item1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Screen J",[NSNumber numberWithInt:3],nil]
                                                  forKeys:[NSArray arrayWithObjects:@"Title",@"View",nil]];

第三个问题是关于我的可能途径我的应用程序。 我有一个XML解析器既节约了不同阵列中的某些信息。 我想利用这个信息,我钻UITableviews(infoFirstScreen [] infoSecondScreen [] infoThirdScreen [])。 提供的信息必须连接外树形像我给你上面。 这是我想建立字典中的代码,所以我可以从我的阵列信息并插入它的原因。 我的问题,你认为我的做法是正确的,错误的或者是有一个更快的方法?

再次真正体会到了解释;)



文章来源: Create a dictionary property list programmatically