gpt4 book ai didi

ios - 在对象数组上循环并显示项目而不重复它们

转载 作者:行者123 更新时间:2023-11-30 11:32:46 25 4
gpt4 key购买 nike

我有一个包含地区和城市的对象数组..这些地区和城市可以在多个对象中重复..

我想循环显示这些地区和城市而不重复它们......

我该怎么做?

这是类(class):

  struct Shifts : Decodable{
let id: Int
let region: String
let city: String
let nationality: String
let idService: Int
let shiftDate: String
let shiftType: String
let weekday: String
let quantityStaff: Int
let leadHours: Int
let createdAt: String?
let updatedAt: String?
let deletedAt: String?
}

数组示例:

shift=[(id: 1, region: Eastern, city: Dammam, nationality: Saudi, idService: 1, shiftDate: 11-12-2019, shiftType: day, weekday: sat, quantityStaff: 1, leadHours: 7),(id:2, region: eastern, city: dhahran, nationality: saudi, idService: 2, shiftDate: 22-1-12018, shiftType: full, weekday: mon, quantityStaff: 2, leadHours: 4)]

两个类次都在东部地区..所以我不想为用户显示东部两次...

我会将它们显示在表格 View 中作为下拉列表,以便用户可以从中进行选择..并且我不想为他们重复它..我只想显示一次。

最佳答案

struct Shifts : Decodable{
let id: Int
let region: String
let city: String
let nationality: String
let idService: Int
let shiftDate: String
let shiftType: String
let weekday: String
let quantityStaff: Int
let leadHours: Int
let createdAt: String?
let updatedAt: String?
let deletedAt: String?
}

struct Area: Hashable{
var region : String
var city : String

var hashValue: Int {
return "(\(region),\(city))".hashValue
}

static func == (lhs: Area, rhs: Area) -> Bool {
return lhs.region == rhs.region && lhs.city == rhs.city
}
}

let shiftsArr = [Shifts]()

var seen = Set<Area>()
var unique = [Shifts]()
for shift in shiftsArr {
let area = Area(region: shift.region, city:shift.city)
if !seen.contains(area) {
unique.append(shift)
seen.insert(area)
}
}

let areas = unique.map{
return Area(region: $0.region, city: $0.city)
}

关于ios - 在对象数组上循环并显示项目而不重复它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com