1

我有一个文本标签,当我按下按钮时会改变,标签确实会随着数组的使用而改变,但是我得到的不是数组的值,而是数组的键。

我应该怎么做才能获得价值?

这是代码:

    NSString*path = [[NSBundle mainBundle]pathForResource:@"words" ofType:@"plist"];
    words = [[NSMutableArray alloc]initWithContentsOfFile:path];
    NSString*generateRandomLabel =[NSString stringWithFormat:@"%d", arc4random_uniform([ words count])];
    [self.randomLabel setText:generateRandomLabel]; 
4

1 回答 1

0
NSString*path = [[NSBundle mainBundle]pathForResource:@"words" ofType:@"plist"];
words = [[NSMutableArray alloc]initWithContentsOfFile:path];
NSString*generateRandomLabel = nil;
if ([words count] >0)
    generateRandomLabel = [NSString stringWithFormat:@"%@", [words objectAtIndex:arc4random_uniform([ words count]-1)]]; // To make sure object is not out of bounds of array add -1
else
    generateRandomLabel = @"No values in array";
[self.randomLabel setText:generateRandomLabel]; 
于 2012-12-05T14:57:36.877 回答