30 Nov 2017

How to Shuffle an Array in Swift...

If you want to shuffle an array use the following method pass an array get the Shuffled Array,


self.shuffledArray = self.ShuffleArray(answersArray: self.answersArray)


    func ShuffleArray(answersArray:[String]) -> [String] {
        var items = answersArray
        var shuffled = [String]();
        
        for _ in 0..<items.count
        {
            let rand = Int(arc4random_uniform(UInt32(items.count)))
            
            shuffled.append(items[rand])
            
            items.remove(at: rand)
        }
        
        return shuffled

    }


No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0