0

我正在尝试显示一个随机的 plist 字符串,但是当我 NSLog 显示的所有内容都是“(null)”时,也许我在代码中做错了:

- (void) viewDidLoad
{

NSString *path = [[NSBundle mainBundle] pathForResource:

                  @"small" ofType:@"plist"];



NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSMutableArray *plistArray = plistDict[@"Value"];

NSInteger randV = arc4random_uniform(plistArray.count); // randV is from 0 to number of strings -1 in array

[super viewDidLoad];

self.correctWord = [plistArray objectAtIndex:randV];

[self setupHangmanWord:self.correctWord];
}

这是我的 small.plist 文件: image

任何帮助都会很棒..谢谢

4

1 回答 1

0

这是不正确的:

NSMutableArray *plistArray = plistDict[@"Value"];

您显示屏幕截图的属性列表有一个数组作为根对象。这行代码假定它是一个键为 的字典Value

你在这里有一个额外的步骤,你不需要。您正在尝试从字典中获取数组,但是您从数组开始并且没有字典。

你应该使用这个:

NSArray *plistArray = [[NSArray alloc] initWithContentsOfFile:path];
于 2013-07-22T16:50:07.477 回答