简体   繁体   中英

NSMutableSet vs NSMutableArray

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?

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.

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.

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.

Well there are 3 main differences. 1) Sets are unordered 2)Don't have an index & 3)Cannot have duplicate values where Arrays are ordered, indexed & can store duplicate values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM