0

基本上我需要从 plist 中获取一个随机字母和与该字母相关的点。老实说,我不确定我是否以最好的方式设置了我的 plist,我以前从未使用过 plist: 在此处输入图像描述

一旦我得到这个工作,我将把 CZ 添加到 plist 中,每个字母关联点。我刚刚创建了一个基本的文字拼图游戏来尝试学习精灵套件。

到目前为止,我已经能够访问 plist 文件,但没有任何运气得到一个随机的信,它是积分。我尝试的一切都遇到了各种错误。

Here's my code so far to access the plist:
        NSString* fileName = @"letters.plist";
        NSString* filepath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];

        NSDictionary* lettersPlist = [NSDictionary dictionaryWithContentsOfFile:filepath];
        NSDictionary* letters = lettersPlist[@"letters"];

那么我如何从这个 plist 中得到一个随机的字母和它的点呢?有没有更好的方法来做到这一点?

4

1 回答 1

4

只需在 0 和数组中的项目数之间获取一个随机数letters,然后取出该信息。例如,继续您的代码:

u_int32_t bound = (u_int32_t)[letters count];
NSDictionary* randomLetterInfo = letters[arc4random_uniform(bound)];
NSString* randomLetter = randomLetterInfo[@"Letter"];
NSString* points = randomLetterInfo[@"Points"]; // points are a string in your plist
于 2013-10-26T20:20:35.323 回答