Reputation: 269
I have an array of dictionary called 'arrAllCustomer'
:
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSSet setWithArray:arrAllCustomer] allObjects]];
After executing above line the array order is changed.
Can anyone know why order is changed?
Upvotes: -1
Views: 923
Reputation: 6775
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
}
}
Upvotes: 2