0

I have a list in my VC

var list : [QCategoryy] = [QCategoryy]()

list = NearbyPlaces.getCategories()
list.shuffled()

where getCategories() is the func

static func getCategories() -> [QCategoryy] {
        let list:[QCategoryy] = [QCategoryy(name: "bar", image: UIImage(named: "bar_button.png")!), QCategoryy(name :"night_club", image: UIImage(named: "nightclub_button.png")!), QCategoryy(name: "movie_theater", image: UIImage(named: "cinema_button.png")!), QCategoryy(name: "restaurant", image: UIImage(named: "restaurant_button.png")!), QCategoryy(name: "gym", image: UIImage(named: "gym_button.png")!), QCategoryy(name: "spa", image: UIImage(named: "spa_button.png")!), QCategoryy(name: "museum", image: UIImage(named: "museum_button.png")!)]
        return list
    }

what i would like to do is to take a randomly number of items from this list and add them to another list like var filteredList = [QCategoryy](), how can i do?

UPDATE

i would like to do it when the user tap a button

arc4Random
  • 101
  • 11

1 Answers1

0

Try this. It will filter the list and each item has 50% chance to be filtered.

var filteredList = list.filter { _ in
    arc4random() % 2 == 0
}

And for shuffling see this answer.

Bilal
  • 18,478
  • 8
  • 57
  • 72