我怎样才能使NSMutableArray只接受唯一值?

时间:2022-06-09 08:53:01
NSMutableArray *sectionTitles;
[sectionTitles addObject:due];

How do I just add unique values to an array?

如何只为数组添加唯一值?

3 个解决方案

#1


28  

See Rudolph's Answer

见鲁道夫的答案

My old answer below is now outdated and has been for awhile. Rudoph's reference to NSOrderedSet / NSMutableOrderedSet is the correct one since these classes were added after this Q and my A.

我下面的旧答案现在已经过时了,已经有一段时间了。 Rudoph对NSOrderedSet / NSMutableOrderedSet的引用是正确的,因为在这个Q和我的A.之后添加了这些类。

Old Answer

老答案

As Richard said, NSMutableSet works well but only if you don't need to maintain an ordered collection. If you do need an ordered collection a simple content check is the best you can do:

正如Richard所说,NSMutableSet运行良好,但前提是您不需要维护有序集合。如果您确实需要有序的收藏,您可以做的最好的简单内容检查:

if (![myMutableArray containsObject:newObject])
    [myMutableArray addObject:newObject];

Update based on comment

根据评论更新

You can wrap this in a method like -addUniqueObject: and put it in an NSMutableArray category.

您可以将它包装在类似-addUniqueObject的方法中:并将其放在NSMutableArray类别中。

#2


19  

More 2012-ish answer (in case someone stumbled upon this in the future):

更多2012-ish的答案(万一有人在将来偶然发现):

Use NSOrderedSet and NSMutableOrderedSet.

使用NSOrderedSet和NSMutableOrderedSet。

A note about performance right from NSOrderedSet docs:

有关NSOrderedSet文档的性能的说明:

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

当元素的顺序很重要时,您可以使用有序集作为数组的替代,并且在测试集合中是否包含对象时的性能是一个考虑因素 - 对数组成员资格的测试比测试集合的成员资格要慢。

#3


5  

Use NSMutableSet, it is best for these situations

使用NSMutableSet,最适合这些情况

iOS Reference

iOS参考

Mac OSX Reference

Mac OSX参考

#1


28  

See Rudolph's Answer

见鲁道夫的答案

My old answer below is now outdated and has been for awhile. Rudoph's reference to NSOrderedSet / NSMutableOrderedSet is the correct one since these classes were added after this Q and my A.

我下面的旧答案现在已经过时了,已经有一段时间了。 Rudoph对NSOrderedSet / NSMutableOrderedSet的引用是正确的,因为在这个Q和我的A.之后添加了这些类。

Old Answer

老答案

As Richard said, NSMutableSet works well but only if you don't need to maintain an ordered collection. If you do need an ordered collection a simple content check is the best you can do:

正如Richard所说,NSMutableSet运行良好,但前提是您不需要维护有序集合。如果您确实需要有序的收藏,您可以做的最好的简单内容检查:

if (![myMutableArray containsObject:newObject])
    [myMutableArray addObject:newObject];

Update based on comment

根据评论更新

You can wrap this in a method like -addUniqueObject: and put it in an NSMutableArray category.

您可以将它包装在类似-addUniqueObject的方法中:并将其放在NSMutableArray类别中。

#2


19  

More 2012-ish answer (in case someone stumbled upon this in the future):

更多2012-ish的答案(万一有人在将来偶然发现):

Use NSOrderedSet and NSMutableOrderedSet.

使用NSOrderedSet和NSMutableOrderedSet。

A note about performance right from NSOrderedSet docs:

有关NSOrderedSet文档的性能的说明:

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

当元素的顺序很重要时,您可以使用有序集作为数组的替代,并且在测试集合中是否包含对象时的性能是一个考虑因素 - 对数组成员资格的测试比测试集合的成员资格要慢。

#3


5  

Use NSMutableSet, it is best for these situations

使用NSMutableSet,最适合这些情况

iOS Reference

iOS参考

Mac OSX Reference

Mac OSX参考