Reputation: 49
i am new to iOS. Anyone know if NSSet
are faster than NSArray
? Why wouldn't I always just use an NSArray
?
Anyone explain me difference between NSSet
and NSArray
?
Upvotes: 1
Views: 8237
Reputation: 25692
NSSet is used to have unique objects.
NSArray may have duplicate objects.
NSSet is an unordered collection.
NSArray is an ordered collection.
Upvotes: 16
Reputation: 711
When the order of elements isn’t important and performance of testing whether an object is in the set is important. Even though arrays are ordered, testing them for membership is slower than testing sets.
Upvotes: 0
Reputation: 4244
When the order of the items in the collection is not important, sets offer better performance for finding items in the collection.
The reason is that a set uses hash values to find items (like a dictionary) while an array has to iterate over its entire contents to find a particular object.
More detailed information here:
http://www.cocoawithlove.com/2008/08/nsarray-or-nsset-nsdictionary-or.html
Upvotes: 0