从 plist 生成随机名称
我想从我创建的 plist 中读取名字和姓氏列表,然后随机选择“Person”类的名字和姓氏。
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"
我已经能够输出字典的所有键,但我不确定如何获取“名字”或“姓氏”键,然后将其存储在数组中。
输出所有键的代码是简单的输出到日志。
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 文件,并且我可以创建随机生成的人。
谢谢。
I'm wanting to read a list of forenames and surnames from a plist I have created, and then randomly select both a forename and a surname for a 'Person' class.
The plist has this structure;
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"
I have been able to output all the keys for the dictionary, but I am unsure of how to grab the 'Forenames' or 'Surnames' key and then store this in an array.
The code to output all the keys is a simple output to the log.
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);
Questions;
How do I make my NSMutableArray *forenames take the contents of the dictionary 'Forenames'?
Once I have stored both the forename and surname in their own NSMutableArrays, I need to randomly select both a forename and a surname; what's the best way to do that?
The idea is that I can create a Person.m file with a Forename/Surname class variables and I can create randomly generated people.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的 plist 的结构,我认为您需要再进行一级取消引用:
然后您可以使用 srandom 和 random 为数组生成随机索引。在 [0,N) 上生成随机数的一般方法是这样的:
NSUInteger i = (NSUInteger)((random()/(double)RAND_MAX)*N);
将上面的 N 替换为调用数组的
count
就可以了。顺便说一句,我看不出创建名称数组的可变副本的原因。我只是想说明一下你会如何做。另外,您需要使用 srandom(time(NULL)); 进行播种,以便在程序每次运行时获得不同的随机值。
Based on the structure of your plist, I think you need to do one more level of dereferencing:
Then you can use
srandom
andrandom
to generate random indices into your arrays. The general way of generating a random number on [0,N) is this:NSUInteger i = (NSUInteger)((random()/(double)RAND_MAX)*N);
Replace N above with a call to an array's
count
and you'll be set.By the way, I can't see a reason for creating a mutable copy of the name arrays. I just wanted to illustrate how you'd do it. Also, you need to seed with
srandom(time(NULL));
to get different random values each time your program runs.