Take the 2-minute tour ×

What's the difference?

In my context, I need to be able to dynamically add to and remove objects. The user clicks on rows of a table that check on and off and thus add or remove the referenced object from the list.

A wild guess is that array has indexed items while set has no indexes?

share|improve this question
Thanks for the remarks! –  Brenden Jul 30 '09 at 16:01
Make sure to upvote the answers that helped you, rather than leave a comment. –  amattn Jul 30 '09 at 20:21
add comment

3 Answers

up vote 26 down vote accepted

An NSSet/NSMutableSet doesn't keep items in any particular order. An NSArray/NSMutableArray does store the items in a particular order. If you're building a table view, you should definitely use an array as your data source of choice.

share|improve this answer
Also, if the order is irrelevant and you frequently ask whether a particular item belongs to the group or not, it is much more efficient to use NSSet rather than NSArray. –  GoRoS Apr 11 at 6:43
add comment

Also, NSMutableSet makes sure that all objects are unique.

NSMutableArray goes well with UITableView since elements have index, so you can return [array count] to get number of table rows, or [array objectAtIndex:rowNumber] to easily associate element with row.

share|improve this answer
This is something I didn't realize –  Brenden Jul 30 '09 at 16:01
add comment

Also, according to the documentation, testing for object membership is faster with NSSets.

You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.

share|improve this answer
add comment

Your Answer


