Get random object from Array
问题 I want to get random object from array, is there any way how can I find random object from mutable array? 回答1: @interface NSArray (Random) - (id) randomObject; @end @implementation NSArray (Random) - (id) randomObject { if ([self count] == 0) { return nil; } return [self objectAtIndex: arc4random() % [self count]]; } @end 回答2: id obj; int r = arc4random() % [yourArray count]; if(r<[yourArray count]) obj=[yourArray objectAtIndex:r]; else { //error message } 回答3: id randomObject = nil; if (