Possible Duplicate:
Picking a Random Object in an NSArray
I have have an array in Objective-C with strings:
NSArray *tips;
tips = [NSArray arrayWithObjects:
@"Foo",
@"Bar",
@"Baz",
nil];
I want a method that takes a random item from the array, and returns it. Is there a method, or how can I write one myself? Thanks.
Use this code:
EDIT: While working on my project i decided to create a category for NSArray. It's very simple but i found it useful.
Here are the files:
NSArray+Random.h
NSArray+Random.m
Then in the current example you use it like this:
Using category has another advantage: when you decide to change your way of selecting random objects in your app you just modify the randomObject method.
Hope this helps