如何在文本标签中获取数组的值而不是键

时间:2012-12-05 14:54:10

标签: iphone objective-c ios arrays label

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

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

这是代码:

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

1 个答案:

答案 0 :(得分: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];