+ (NSArray *)getRandomWithPosition:(NSInteger)position positionContent:(id)positionContent array:(NSArray *)baseArray { NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:baseArray.count]; NSMutableArray *tempBaseArray = [NSMutableArray arrayWithArray:baseArray]; while ([tempBaseArray count]) { NSInteger range = [tempBaseArray count]; id string = [tempBaseArray objectAtIndex:arc4random()%range]; [resultArray addObject:string]; [tempBaseArray removeObject:string]; } // 计算指定数组元素在新数组中的位置 NSUInteger index = [resultArray indexOfObject:positionContent];
// 交换数组元素 [resultArray exchangeObjectAtIndex:index withObjectAtIndex:position]; return resultArray; }