目标 C - NSString 工作,但 plist 字符串不起作用



我有一段代码,它在plist中找到一个随机单词,然后将其分配给NSString'correctWord'。出于某种原因,代码在一个实例中运行良好,但在另一个实例则不起作用。

此代码有效:

- (void) viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"small" ofType:@"plist"];
NSArray *plistArray = [[NSArray alloc] initWithContentsOfFile:path];
NSInteger randV = arc4random_uniform(plistArray.count); // randV is from 0 to number of strings -1 in array
[super viewDidLoad];
self.correctWord = @"Hello"; //<--- Code works fine here..
[self setupHangmanWord:self.correctWord];
}

此代码不起作用。(不过编译器中没有错误)

- (void) viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"small" ofType:@"plist"];
NSArray *plistArray = [[NSArray alloc] initWithContentsOfFile:path];
NSInteger randV = arc4random_uniform(plistArray.count); // randV is from 0 to number of strings -1 in array
[super viewDidLoad];
self.correctWord = [plistArray objectAtIndex:randV]; //<--- Doesn't work anymore.
[self setupHangmanWord:self.correctWord];
}

使用:

NSString *path = [[NSBundle mainBundle] pathForResource:@"small" ofType:@"plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:path];

最新更新