检查NSMutableArray上的重复NSStrings [英] Check for duplicate NSStrings on a NSMutableArray

查看:206
本文介绍了检查NSMutableArray上的重复NSStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSMutableArray,我想添加字符串。我需要检查该字符串是否已在数组上添加ths字符串之前。

I got a NSMutableArray which I want to add strings to. I need to check if that string already exists on the array before adding ths string to it. Does anyway know a good way to do it?

谢谢!

推荐答案

p>如果顺序无关紧要,最简单的方法就是切换到使用NSMutableSet。集合是无序的,NSSets和NSMutableSets只能包含每个对象一次。

If order doesn't matter, the simplest way would be to switch to using an NSMutableSet. Sets are unordered and NSSets and NSMutableSets can contain each object only once. Only a counted set (NSCountedSet) can contain the same object multiple times.

如果顺序无关紧要,继续使用NSMutableArray,并使用 -containsObject:

If order does matter, continue using NSMutableArray, and use -containsObject: to check that the object is not already there before adding it.

编辑:的狮子,我们现在有 NSOrderedSet 和NSMutableOrderedSet!巧克力在我的花生酱!花生酱在我的巧克力!

And as of Lion, we now have NSOrderedSet and NSMutableOrderedSet! Chocolate in my peanut butter! Peanut butter in my chocolate!

像一个无序的集合,你得到快速的成员测试,禁止重复,当然,集合操作(​​联合,交集等。)。像数组一样,您可以获得有序访问,包括可靠顺序枚举和通过索引访问。

Like an unordered set, you get fast membership testing, the prohibition of duplicates, and, of course, set operations (union, intersection, etc.). Like an array, you get ordered access, including both reliable-order enumeration and access by index.

因此,如果您需要拒绝重复,但同时保留顺序,Lion的NS {,Mutable} OrderedSet类是为你。

So, if you need to reject duplicates but at the same time preserve order, Lion's NS{,Mutable}OrderedSet classes are for you.

这篇关于检查NSMutableArray上的重复NSStrings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆