简体   繁体   English

NSMutableSet与NSMutableArray

[英]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? 一个疯狂的猜测是,数组有索引项,而set没有索引?

An NSSet / NSMutableSet doesn't keep items in any particular order. NSSet / NSMutableSet不会以任何特定顺序保留项目。 An NSArray / NSMutableArray does store the items in a particular order. NSArray / NSMutableArray确实按特定顺序存储项目。 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. 此外,NSMutableSet确保所有对象都是唯一的。

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. NSMutableArray适用于UITableView,因为元素具有索引,因此您可以返回[array count]以获取表行数,或者[array objectAtIndex:rowNumber]以轻松地将元素与行相关联。

Also, according to the documentation , testing for object membership is faster with NSSets. 此外,根据文档 ,使用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. 1)集合是无序的2)没有索引&3)不能有重复的值,其中数组是排序的,索引的并且可以存储重复的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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