我有一个文本标签,当我按下一个按钮时,标签会发生变化,标签会随着数组的使用而改变,但是不是数组的值,而是获得数组的键。
我该怎么做才能获得价值?
这是代码:
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];
答案 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];