我的风投中有一个清单

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

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


其中getCategories()是函数

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
    }


我想做的是从此列表中随机抽取一些项目并将其添加到另一个列表,例如var filteredList = [QCategoryy](),我该怎么办?

更新

我想在用户点击按钮时这样做

最佳答案

尝试这个。它将过滤列表,并且每个项目都有50%的机会被过滤。

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


对于改组,请参见this答案。

10-06 01:10