我想从我创建的plist中读取姓氏和姓氏的列表,然后随机选择'Person'类的姓氏和姓氏。从plist生成随机名
plist有这样的结构;
Root (Dictionary)
-> Names (Dictionary)
--> Forenames (Array)
---> Item 0 (String) "Bob"
---> Item 1 (String) "Alan"
---> Item 2 (String) "John"
--> Surnames (Array)
---> Item 0 (String) "White"
---> Item 1 (String) "Smith"
---> Item 2 (String) "Black"
我已经能够输出的字典中的所有键,但我不确定如何抓住“Forenames”或“百家姓”键,然后存储这在数组中。
输出所有密钥的代码是简单的日志输出。
ie;
// Make a mutable (can add to it) dictionary
NSMutableDictionary *dictionary;
// Read foo.plist
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"foo.plist"];
dictionary = [NSDictionary dictionaryWithContentsOfFile:finalPath];
// dump the contents of the dictionary to the console
for (id key in dictionary)
{
NSLog(@"Bundle key=%@, value=%@", key, [dictionary objectForKey:key]);
}
NSMutableArray *forenames;
// This doesn't work, it outputs an empty array
forenames = [dictionary objectForKey:@"Forenames"];
NSLog(@"Forenames:%@", forenames);
问题;
如何让我的NSMutableArray * forenames获取字典'Forenames'的内容?
一旦我已经将自己的名字和姓氏存储在他们自己的NSMutableArrays中,我需要随机选择一个名字和一个姓氏;什么是最好的方式来做到这一点?
这个想法是我可以用Forename/Surname类变量创建一个Person.m文件,我可以创建随机生成的人。
谢谢。
我认为这是隐含的,但字典内容的转储是否按预期显示?所有这些代码都是同一个方法的一部分吗?另外,值得注意的是,存储在新创建的字典中的数组是不可变的,所以如果你想修改它们,你必须在它们上面调用'mutableCopy'。 – Endemic 2010-06-22 17:08:59