NSSet属于集合,它与NSArray都是用来存储对象,并且它只能存储不同的对象,其对象具有唯一性。NSSet和NSMutableSet声明的对像是无序的集合,在内存中存储方式是不连续的,这点与NSArray和NSDictionary不同,NSArray和NSDictionary声明的对象是有序集合,在内存中存储的位置是连续的。
在官方文档中也提到,NSSet与NSArray的区别:If the behavior you want to add supplements that of the existing class, you could write a category on NSSet
. Keep in mind, however, that this category will be in effect for all instances of NSSet
that you use, and this might have unintended consequences. Alternatively, you could use composition to achieve the desired behavior.所以,如果需要对该集合对象搜索时,NSSet比NSArray的效率要高。因为它使用了一个hash算法。
NSSet 常用方法总结
+(id)setWithObjects:obj1,obj2,...nil | 使用一组对象创建新的集合 |
-(id)initWithObjects:obj1,obj2,....nil | 使用一组对象初始化新分配的集合 |
-(NSUInteger)count | 返回集合成员个数 |
-(BOOL)containsObject:obj | 确定集合是否包含对象 obj |
-(BOOL)member:obj | 确定集合是否包含对象 obj |
-(NSEnumerator*)objectEnumerator | 返回集合中所有对象到一个 NSEnumerator 类型的对象 |
-(BOOL)isSubsetOfSet:nsset | 判断集合是否是NSSet的子集 |
-(BOOL)intersectsSet:nsset | 判断两个集合的交集是否至少存在一个元素 |
-(BOOL)isEqualToSet:nsset | 判断两个集合是否相等 |
NSMutableSet 常用方法总结
-(id)setWithCapcity:size | 创建一个有size大小的新集合 |
-(id)initWithCapcity:size | 初始化一个新分配的集合,大小为size |
-(void)addObject:obj | 添加对象 obj 到集合中 |
-(void)removeobject:obj | 从集合中删除对象 obj |
-(void)removeAllObjects | 删除集合中所有对象 |
-(void)unionSet:nsset | 将nsset的所有元素添加到集合 |
-(void)minusSet:nsset | 从集合中去掉所有的NSSet 的元素 |
-(void)interectSet:nsset |