从 plist 生成随机名称

发布于 2024-09-06 11:26:54 字数 1384 浏览 2 评论 0原文

我想从我创建的 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);

问题;

  1. 如何让我的 NSMutableArray *forenames 获取字典“Forenames”的内容?

  2. 一旦我将名字和姓氏存储在自己的 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;

  1. How do I make my NSMutableArray *forenames take the contents of the dictionary 'Forenames'?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-09-13 11:26:54

根据您的 plist 的结构,我认为您需要再进行一级取消引用:

NSDictionary *names = [dictionary objectForKey:@"Names"];
NSMutableArray *forenames = [[names objectForKey:@"Forenames"] mutableCopy];
NSMutableArray *surnames = [[names objectForKey:@"Surnames"] mutableCopy];

然后您可以使用 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:

NSDictionary *names = [dictionary objectForKey:@"Names"];
NSMutableArray *forenames = [[names objectForKey:@"Forenames"] mutableCopy];
NSMutableArray *surnames = [[names objectForKey:@"Surnames"] mutableCopy];

Then you can use srandom and random 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文