+2 votes
in C Plus Plus by
When to use NSArray vs NSSet?

1 Answer

0 votes
by

NSArray is faster than NSSet for simply holding and iterating. As little as 50% faster for constructing and as much as 500% faster for iterating. So if you only need to iterate contents, don't use an NSSet.

Of course, if you need to test for inclusion, work hard to avoid NSArray. 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. Even if you need both iteration and inclusion testing, you should probably still choose an NSSet. If you need to keep your collection ordered and also test for inclusion, then you should consider keeping two collections (an NSArray and an NSSet), each containing the same objects.

Related questions

+2 votes
asked Jan 19, 2022 in C Plus Plus by DavidAnderson
+1 vote
asked Dec 18, 2022 in Azure by rajeshsharma
...