我有一个具有区域和城市的对象数组..这些区域和城市可以在多个对象中重复..
我想循环播放它们并显示这些地区和城市而不重复它们......
我该怎么做?
这是班级:
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,地区:东部,城市:达曼,国籍:沙特阿拉伯, idService:1,shiftDate:11-12-2019,shiftType:day,weekday:sat, quantityStaff:1,leadHours:7),(id:2,地区:东部,城市:dhahran, 国籍:saudi,idService:2,shiftDate:22-1-12018,shiftType: 完整,工作日:星期一,数量工作人员:2,工作时间:4)]
我会在桌面视图中显示它们作为下拉列表,以便用户可以从中进行选择..我不想为它们重复它...我只想显示一次。
答案 0 :(得分:0)
您可以使用一组来过滤具有唯一区域的移位:
let rec printTable aTable = match aTable with
( )->""
| (title,[data]) -> "\n"^title^"\n\n"^printTable(data)
| [(col,cont)::t] -> col^" "^printTable([t])
答案 1 :(得分:0)
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)
}