I have an array which has repeated items, i want to get unique items only so i did this:
let decoded = userDefaults.object(forKey: "shifts") as! Data
myShifts = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Shift]
filtered_shifts = myShifts.filter{$0.regionName == region && $0.cityName == city && $0.idService == idservice && $0.quantityStaff != 0}
for shift in filtered_shifts {
let decoded4 = userDefaults.object(forKey: "nationalities") as! Data
let decodedNationalities = NSKeyedUnarchiver.unarchiveObject(with: decoded4) as! [Nationality]
for nationality in decodedNationalities {
if nationality.id == shift.idNationality{
nationalitiesid.append(nationality.id)
nationalities.append(nationality.name)
}
}
}
uniqueNationality = Array(Set(nationalities))
uniqueNationalityid = Array(Set(nationalitiesid))
which is getting me a new array with unique items as i need...
the problem is that the order is diferent when i want the same order ... for exampple:
nationalitiesid = [3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4]
nationalities = ["Indian", "Indian", "Indian", "Indian", "Indian", "Indian", "Indian", "Indian", "Philippines", "Philippines", "Philippines", "Philippines"]
the unique array is:
uniqueNationality = ["Indian", "Philippines"]
uniqueNationalityid = [4, 3]
and this is wrong since Indian id is 3 and Philippines id is 4!
it should be:
uniqueNationalityid = [3, 4]
How to solve this?