Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从plist文件的字典中嵌套的数组中访问随机值?_Ios_Objective C_Plist - Fatal编程技术网

Ios 如何从plist文件的字典中嵌套的数组中访问随机值?

Ios 如何从plist文件的字典中嵌套的数组中访问随机值?,ios,objective-c,plist,Ios,Objective C,Plist,基本上,我需要从plist中获取一个随机字母和与该字母相关的点。 老实说,我不确定我是否以最好的方式设置了plist,我以前从未使用过plist: 一旦我完成这项工作,我将在plist中添加C-Z,每个字母都与点相关。我刚刚创建了一个基本的word tile游戏来尝试学习精灵工具包 到目前为止,我已经能够访问plist文件,但没有任何运气得到一个随机的字母和它的分数。我得到了各种各样的错误与我的尝试 Here's my code so far to access the plist:

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

一旦我完成这项工作,我将在plist中添加C-Z,每个字母都与点相关。我刚刚创建了一个基本的word tile游戏来尝试学习精灵工具包

到目前为止,我已经能够访问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中得到一个随机的字母和它的点数呢?还有更好的方法吗?

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

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

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

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

美好的我只是按照你的建议做了,然后不得不更改指向NSArray的字母指针,这就成功了。@Scotty哦,对不起,我甚至没有在你的示例代码中看到它是一个
字典,因为它在你的plist中是一个
数组
)对不起,没问题。我收到的错误很明显是我需要做的。很好!我只是按照你的建议做了,然后不得不更改指向NSArray的字母指针,这就成功了。@Scotty哦,对不起,我甚至没有在你的示例代码中看到它是一个
字典,因为它在你的plist中是一个
数组
)对不起,没问题。由于我收到的错误,很明显我需要做什么。