以编程方式创建字典属性列表
我想以编程方式创建一个字典,将数据提供给我的 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 开发还是个新手,所以请耐心等待。 ;)
I want to programatically create a dictionary which feeds data to my UITableView but I'm having a hard time with it. I want to create a dictionary that resembles this property list (image) give or take a couple of items.
I've looked at "Property List Programming Guide: Creating Property Lists Programmatically" and I came up with a small sample of my own:
//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];
I'm trying to use this data of hierachy for my table views.
So I was wondering how can I can create my property list (dictionary) programmatically so that I can fill it with my own arrays.
I'm still new with iPhone development so bear with me. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先..超级! 谢谢..我真的很感谢你的解释和代码片段。
既然你给了我这么好的解释,我希望你不介意我再问几个问题。
首先,我按照你的建议做了,这就是我想到的:
(这次我使用了原始属性列表而不是示例,因此这是钻取表获取他的(或需要获取他的)树结构的地方)。
http://img509.imageshack.us/img509/7523/picture2lsg.png
还做了一些研究,并尝试了一些其他的输入。
我决定只测试这两个,但它确实达到了我想要的效果。 它给了我一个
EXC_BAD_ACCESS
错误。我也想知道,因为我看到您在代码片段中使用了数字,您是否也可以使用 NSString 因为这就是 plist 使用的内容..当然可以完全在这里,
第三个问题是关于我对我的应用程序的可能方法。
我有一个 xml 解析器,它将某些信息保存在不同的数组中。
我想将此信息用于我的深入 UITableviews (infoFirstScreen[] infoSecondScreen[] infoThirdScreen[])。
所提供的信息必须像我上面向您展示的树一样连接起来。
这就是我想在代码中构建字典的原因,这样我就可以从数组中获取信息并将其插入此处。
我的问题你认为我的方法是正确的、错误的还是有更快的方法?
再次非常感谢您的解释;)
first of .. super! thanks .. I really appreciate the explanation and code snippet.
Since you gave me such a good explanation I hope you don't mind me asking a couple more questions.
First, I did as you suggested and this is what I came up with :
(I used my original property list instead of the example this time so this is where the drilldown table gets his( or needs to get his ) treestructure).
http://img509.imageshack.us/img509/7523/picture2lsg.png
Also did some research and tried something else with some other input..
I decided to just test both of these however it does what I want. It gives me a
EXC_BAD_ACCESS
error.I was also wondering since I saw you using number in your code snippet, couldn't you also use NSString since that's what the plist uses.. could be totally of here of course
and third a question is about my possible approach to my app.
I have an xml parser which saves certain information in different arrays.
I want to use this information for my drilldown UITableviews (infoFirstScreen[] infoSecondScreen[] infoThirdScreen[]).
The information provided has to be connected like the tree I showed you above.
This is the reason I wanted to build the dictionary in code so I can take the info from my arrays and insert it here.
My question do you think my approach is correct, wrong or is there a faster way?
again really appreciate the explanation ;)
在这种情况下,“授人以鱼”比“授人以鱼”更有帮助。 一旦您了解了基本原理和 NSDictionary API,构建您自己的自定义解决方案就会变得更加容易。 以下是一些观察和学习点:
+dictionaryWithObject:forKey:
用于创建具有单个键值对的 NSDictionary。 它不接受方法调用中每个冒号 (:) 后面的逗号分隔参数,仅接受一个。 要创建具有多个键值对的字典,请使用 2 个相关方法之一:+dictionaryWithObjects:forKeys:
接受两个包含值和键的 NSArray 对象,或 < a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDictionary/dictionaryWithObjectsAndKeys:" rel="noreferrer" >+dictionaryWithObjectsAndKeys:
它将 (object, key, object, key) 与终止nil
参数交替。-copy
就可以了。 (请记住,复制方法返回一个新对象,您必须释放该对象以避免内存泄漏。)这样您就不必为每个值都有一个变量,这样您就可以“一次性”创建结构( )在每个级别。+arrayWithObject:
用于创建具有单个对象的 NSArray。只是为了让您了解在链接图像中创建字典在代码中可能是什么样子...(我忽略您选择的变量名称并使用两种不同的方法来保证完整性。)
This is a situation where "teach a man to fish" is a vastly more helpful approach than "give a man a fish". Once you understand the basic principles and the NSDictionary API, it becomes much easier to craft your own custom solution. Here are a few observations and learning points:
+dictionaryWithObject:forKey:
is used to create an NSDictionary with a single key-value pair. It will not accept comma-separated arguments after each colon (:) in the method call, just one. To create a dictionary with multiple key-value pairs, use one of 2 related methods:+dictionaryWithObjects:forKeys:
which accepts two NSArray objects containing values and keys, or+dictionaryWithObjectsAndKeys:
which alternates (object, key, object, key) with a terminatingnil
argument.-copy
on it. (Remember that a copy method returns a new object that you must release to avoid memory leaks.) That way you don't have to have a variable for every single value so you can do a "one shot" creation of the structure(s) at each level.+arrayWithObject:
for creating an NSArray with a single object.Just to give you a flavor of what creating the dictionary in the linked image might look like in code... (I'm ignoring your chosen variable names and using both different approaches for completeness.)
我不确定我是否理解这里的基本目标。
看起来在运行时,您正在构建一个包含许多子节点的深层字典。 但是你正在用静态代码构建这一切......为什么你不能简单地创建一个plist(就像你有图像的那个)并将其读入NSDictionary? NSDictionary 和 NSArray 都有方法让你简单地读入一个文件并获得一个完整的填充对象。 然后编辑和理解就更容易了。 该方法是
dictionaryWithContentsOfFile
。如果所有数据在放入字典之前确实是在运行时创建的,那么您似乎需要一种非常不同的递归代码风格,而不是给出的平面示例。
最后,我个人不喜欢 NSDictionary 中用于构建字典的
dictionaryWithObjects:forKeys:
方法。 如果你必须这样做,我非常喜欢替代方法dictionaryWithObjectsAndKeys:
,它做同样的事情,但将键与对象一起保存:I am not sure I understand the basic objective here.
It seems like at runtime, you are constructing a deep dictionary with many child nodes. But you are constructing this all with static code... why can you not simply make a plist (like the one you had an image of) and read that into an NSDictionary? Both NSDictionary and NSArray have methods that let you simply read in a file and get a whole filled out object. Then it is WAY easier to edit and to understand. That method is
dictionaryWithContentsOfFile
.If all of the data is truly created at runtime before it is put into the dictionary, then it seems like you would want a very different, recursive, style of code rather than the flat examples given.
Lastly, I personally dislike the
dictionaryWithObjects:forKeys:
method in NSDictionary for building a dictionary. If you have to do things that way I greatly prefer the alternate methoddictionaryWithObjectsAndKeys:
which does the same thing but keeps the keys with the objects: