以编程方式创建字典属性列表 [英] Create a dictionary property list programmatically

查看:57
本文介绍了以编程方式创建字典属性列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式创建一个字典,该字典将数据馈送到我的UITableView中,但是我很难.我想创建一个类似于的字典此属性列表(图片)给出或接受几个项目.

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];

我正在尝试将hierachy的这些数据用于我的表格视图.

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.

我对iPhone的开发还很陌生,所以请耐心等待. ;)

I'm still new with iPhone development so bear with me. ;)

推荐答案

在这种情况下,教人钓鱼"是一种比给人鱼钓鱼"更为有用的方法.一旦了解了基本原理和NSDictionary API,就可以轻松制作自己的自定义解决方案.以下是一些观察和学习要点:

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:

  • 方法 NSMutableDictionary在代码中构建字典,然后(如有必要)调用
  • 使用 +arrayWithObject: 用于创建具有单个对象的NSArray.
  • (样式建议)切勿使用大写字母开头变量,方法或函数的名称. (请注意,SO会突出显示诸如类名之类的大写字母变量.)这肯定会帮助其他阅读您的代码的人不会因您的命名方案而对您的意图感到困惑.
  • The method +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 terminating nil argument.
  • Simplify the creation code. You don't need a million local variables just to create the thing. Use inline arguments where it makes sense. One way to do this it to build up your dictionary in code as an NSMutableDictionary, then (if necessary) make an immutable copy of it by calling -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.
  • Use +arrayWithObject: for creating an NSArray with a single object.
  • (Style suggestions) Never use an uppercase letter to begin the name of a variable, method, or function. (Notice that SO highlights leading-caps variables like class names.) It will certainly help others who read your code from being confused about your intent by your naming scheme.

只是为了让您了解在链接的图像中创建字典的方式在代码中的样子……(我忽略了您选择的变量名,并使用两种不同的方法来保证完整性.)

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.)

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];
...

这篇关于以编程方式创建字典属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆