vendredi 19 mars 2021

how to randomize items and then remove from a Swift array

I have an array of fruits. I am fetching a fruit randomly (see following codes). Questions: How do I remove this fruit from the array after fetching it?

    let fruits = ["apple", "orange", "banana", "pineapple", "lemon" ]
    
    func getRandomFruit() -> String {
        let randomFruit = fruits.randomElement()!
        return randomFruit
    }



Aucun commentaire:

Enregistrer un commentaire