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 に答える