How do I shuffle an array in Swift?

前端 未结 25 2270
长发绾君心
长发绾君心 2020-11-21 05:44

How do I randomize or shuffle the elements within an array in Swift? For example, if my array consists of 52 playing cards, I want to shuffle the array in o

25条回答
  •  春和景丽
    2020-11-21 06:29

    If you want to use simple Swift For loop function use this ->

    var arrayItems = ["A1", "B2", "C3", "D4", "E5", "F6", "G7", "H8", "X9", "Y10", "Z11"]
    var shuffledArray = [String]()
    
    for i in 0..

    Swift Array suffle using extension ->

    extension Array {
        // Order Randomize
        mutating func shuffle() {
            for _ in 0..

提交回复
热议问题