Randomly select from an array of strings in Objective-C?

时间:2019-04-17 01:39:07

标签: objective-c

I have done some digging, and I don't believe this question has been answered yet (though I would imagine it would be simple).

I want to select a random string from an array. For example:

    NSArray *strings = @[@"String1", @"String2", @"String3"];
    NSString *randomString = // how to randomly select one of the three strings in the array?

1 个答案:

答案 0 :(得分:0)

You want arc4random_uniform():

NSString *randomString = strings[arc4random_uniform(strings.count)];

More info in this SO answer.