10 iphone objective-c nsdictionary
我想以编程方式创建一个字典,将数据提供给我的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];
Run Code Online (Sandbox Code Playgroud)
我正在尝试将这个hierachy数据用于我的表视图.
所以我想知道如何以编程方式创建我的属性列表(字典),以便我可以用自己的数组填充它.
我仍然是iPhone开发的新手,所以请耐心等待.;)
Qui*_*lor 24
在这种情况下,"教人钓鱼"比"给男人钓鱼"更有帮助.一旦理解了基本原理和NSDictionary API,就可以更轻松地制作自己的自定义解决方案.以下是一些观察和学习要点:
+dictionaryWithObject:forKey:用于创建具有单个键值对的NSDictionary.在方法调用中的每个冒号(:)之后,它不会接受以逗号分隔的参数,只有一个.要创建具有多个键值对的字典,请使用以下两种相关方法之一:+dictionaryWithObjects:forKeys:它接受两个包含值和键的NSArray对象,或者+dictionaryWithObjectsAndKeys:使用终止nil参数替换(对象,键,对象,键).-copy它来创建它的不可变副本.(请记住,复制方法返回一个新的对象,您必须释放,以避免内存泄漏.)你不必这样,有一个变量的每一个值,所以你可以做结构的"一点击"创建(S )在每个级别.+arrayWithObject:了与单个对象创建一个NSArray.只是为了让您了解在链接图像中创建字典的内容可能在代码中看起来...(我忽略了您选择的变量名称并使用两种不同的方法来完成.)
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];
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29119 次 |
| 最近记录: |