简体   繁体   English

循环对象数组并显示项目,而无需复制它们

[英]looping on array of object and display items without duplicating them

i have an array of object which has region and city.. these region and city can be repeated in more than one object .. 我有一个具有区域和城市的对象数组。这些区域和城市可以在多个对象中重复。

i want to loop on them and display these regions and cities without duplicating them ... 我想循环播放它们并显示这些区域和城市,而不必重复它们...

how can i do this? 我怎样才能做到这一点?

this is the 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?
}

example of the array: 数组的示例:

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)] shift = [((id:1,地区:东部,城市:达曼,国籍:沙特,id)服务:1,shiftDate:11-12-2019,shiftType:day,Weekday:sat,QuantityStaff:1,leadHours:7),( id:2,地区:东部,城市:dhahran,国籍:沙特,id服务:2,班次日期:22-1-12018,shiftType:完整,工作日:星期一,数量工作人员:2,leadHours:4)]

both shifts are in region eastern .. so i don't want to display eastern twice for the user... 两个班次都在东部地区..所以我不想为用户两次显示东部地区...

i will display them in a tableview as dropdown list so the user can select from it .. and i don't want to repeat it for them .. i only want to display it once. 我将它们在表格视图中显示为下拉列表,以便用户可以从中选择..我不想为他们重复..我只想显示一次。

You could use a set to filter Shifts with unique regions: 您可以使用一个集合来过滤具有唯一区域的Shift:

let myShifts = [Shifts]()
//populate your myShifts array.

func filteredShifts() -> [Shifts] {

    var regions = Set<String>()

    let filteredShifts = myShifts.filter {

      if !regions.contains($0.region) {
        regions.insert(aShift.region)
        return true
      } else { return false }
    }
    return filteredShifts
}
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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM