Objective C - NSString工作但plist字符串不起作用

时间:2013-07-22 19:13:59

标签: objective-c nsstring plist

我有一段代码在plist中找到一个随机单词,然后将它分配给NSString'correctionWord'。出于某种原因,代码在一个实例中工作正常但在另一个实例中不起作用。

此代码有效:

- (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];
}

1 个答案:

答案 0 :(得分:0)

使用:

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