swift 如何set去重

2024-04-09  本文已影响0人  可乐小子

let dataArr = self.withdrowArray.flatMap({$0.items }) + array
var setData = Set<String>()
var nowArr = SectionData

                for model in dataArr {
                    //去重,去除重复月份
                    if !setData.contains(model.showTime) {
                        setData.insert(model.showTime)
                        
                        var modelArr = [CR_WithdrawModel]()
                        if let items = nowArr.first(where: { $0.time == model.showTime })?.items {
                            modelArr = items
                        }
                        modelArr.append(model)
                        nowArr.append(SectionData(time: model.showTime, items: modelArr))
                    } else {
                        if let section = nowArr.firstIndex(where: { $0.time == model.showTime }) {
                            var items = nowArr[section].items
                            items.append(model)
                            nowArr[section].items = items
                        } else {                            nowArr.append(SectionData(time: model.showTime, items: [model]))
                        }
                    }
                }
                self.withdrowArray = nowArr
上一篇下一篇

猜你喜欢

热点阅读