NSSet
is not supposed to retain order.
From Apple Doc:
The NSSet,
NSMutableSet
, and
NSCountedSet
classes declare the programmatic interface to an unordered collection of objects.
If you want to retain the order of objects, work with NSArray:
NSArray and its subclass
NSMutableArray
manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects.
If you want to remove duplicate objects from the array, you can write a function yourself. An example taken from here:
extension Array where Element: Equatable {
/// Array containing only _unique_ elements.
var unique: [Element] {
var result: [Element] = []
for element in self {
if !result.contains(element) {
result.append(element)
}
}
return result
}
}