[英]How to initialize a NSMutableDictionary of a NSArray of NSStrings from a .plist file?
For example, the dictionary of array of strings, for who likes what
: 例如,
who likes what
的字符串数组字典:
@"John" -> [@"Books", @"TV", @"Poker", nil]
@"Peter" -> [@"Programming", @"Simsons", nil]
@"Tim" -> [@"Travel", @"Photography", nil]
How can I initialize the NSMutableDictionary
for above, say, the data is from a .plist
? 我该如何初始化上述的
NSMutableDictionary
,例如,数据来自.plist
? Thanks! 谢谢!
Well, if you simply invoke -writeToFile:
on the NSDictionary
holding the NSArray
instances, it will write it out to a plist file that you specify. 好吧,如果您只是在保存
NSArray
实例的NSDictionary
上调用-writeToFile:
它将把它写到您指定的plist文件中。
Then, you can use -initWithContentsOfFile:
to re-create that same NSDictionary
, holding the same NSArray
data as well. 然后,您可以使用
-initWithContentsOfFile:
重新创建相同的NSDictionary
,同时保留相同的NSArray
数据。
Create an empty plist file in your project. 在您的项目中创建一个空的plist文件。
Set the root initial value to dictionary. 将根初始值设置为dictionary。
Add each person as a key and set the value type to array. 将每个人添加为键,并将值类型设置为array。 (right click)
(右键点击)
Press the expand button (little plus on right of selected line) to get Item0, Item1, etc for each person. 按下扩展按钮(在所选行的右边加小写)以获取每个人的Item0,Item1等。 Set the value for each of these.
设置每个的值。 (Books, TV, Poker, etc)
(书籍,电视,扑克等)
Save the file. 保存文件。
Use 采用
NSString *path = [[NSBundle mainBundle] pathForResource:@"<plist name w/out extension" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.