Swift语言Array扩展Shuffle方法

#Playground代码

// Playground - noun: a place where people can play
// 戴维营教育  Swift Array数组 简单洗牌函数实现
// demonstrated by 大茶园丁.


import Cocoa


//全局函数形式
func ç<T : Comparable>(arr:T[]) -> Void{
    sort(arr, { (a1: T , a2: T) -> Bool in
        var a: UInt32 = arc4random()%3
        return a >= 1
        })

}
var array = [1,2,3,4,5,6,7,8,9,10,11,12,13]
shuffle(array)

array


//数组的扩展方法形式
extension Array {
    func shuffle(){
        self.sort({(a1, a2) -> Bool in
            var a: UInt32 = arc4random()%3
            return a >= 1
            })
    }
}
var aa = ["~!@~","qwe","sdf","132","354345", "bvnccnbvncvn"]

aa.shuffle()




标签:none

添加新评论