1. 程式人生 > >ios 陣列打亂順序

ios 陣列打亂順序

-(NSMutableArray*)getRandomArrFrome:(NSArray*)arr
{
    NSMutableArray *newArr = [NSMutableArray new];
    while (newArr.count != arr.count) {
        //生成隨機數
        int x =arc4random() % arr.count;
        id obj = arr[x];
        if (![newArr containsObject:obj]) {
            [newArr addObject:obj];
        }
    }
    
return newArr; }

 

NSArray+YYAdd 這個shuffle方法不知道行不行

/**
 Sort the object in this array randomly.
 */
- (void)shuffle;