How to split an array in half in Swift?

前端 未结 5 1765
醉梦人生
醉梦人生 2021-02-06 23:26

How do I split a deck of cards? I have an array made and a random card dealer, but have no idea how to split the deck.

Thanks everyone for the help! I now have a working

5条回答
  •  爱一瞬间的悲伤
    2021-02-07 00:33

    Swift

    More generic solution to split the array into chunks the answer from this link

    extension Array {
        func chunked(into size: Int) -> [[Element]] {
            return stride(from: 0, to: count, by: size).map {
                Array(self[$0 ..< Swift.min($0 + size, count)])
            }
        }
    }
    let numbers = Array(1...100)
    let result = numbers.chunked(into: 5)
    

提交回复
热议问题