您的位置:首页 > 移动开发 > IOS开发

iOS NSSet和NSMutableSet

2016-04-05 15:12 483 查看
一、简介

集合(set)是一组单值对象,它可以是固定的(NSSet)、也可以是可变的(NSMutableSet)。集合可以比较、计算交集、并集,可变集合还可以有查找、添加、删除。

二、常用方法

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])

{

NSAutoreleasePool*pool=[[NSAutoreleasePool alloc]init];

//集合比较、修改

NSSet *set1=[NSSet setWithObjects:@”s1″,@”s2″,@”s3″,@”s4″,nil];

NSMutableSet *set2=[NSMutableSet setWithObjects:@”s1″,@”s2″,@”s3″,@”s4″,nil];

NSLog(@”List set1″);

for (NSString *element in set1) {

NSLog(@”%@”,element);

}

if ([set1 isEqualToSet:set2]) {

NSLog(@”set1 is equal to set set2″);

} else {

NSLog(@”set1 is not equal to set set2″);

}

if ([set1 containsObject:@”s1″]) {

NSLog(@”set1 contains s1″);

} else {

NSLog(@”set1 not contains s1″);

}

[set2 addObject:@”s5″];

[set2 removeObject:@”s3″];

NSLog(@”List set2″);

for (NSString *element in set2) {

NSLog(@”%@”,element);

}

//集合交集、并集

NSMutableSet *set3;

set3=[NSMutableSet setWithObjects:@”s1″,@”s3″,@”s5″,nil];

[set3 intersectSet:set1];

NSLog(@”135 intersectSet 1234″);

for (NSString *element in set3) {

NSLog(@”%@”,element);

}

set3=[NSMutableSet setWithObjects:@”s1″,@”s3″,@”s5″,nil];

[set3 unionSet:set1];

NSLog(@”135 unionSet 1234″);

for (NSString *element in set3) {

NSLog(@”%@”,element);

}

[pool drain];

return 0;

}

setWithObjects创建包含给定的对象列表的集合

+ (id)setWithObjects:(id)firstObj …

isEqualToSet比较两个集合是否相等

– (BOOL)isEqualToSet:(NSSet *)otherSet

containsObject判断给定的对象是否在集合中

– (BOOL)containsObject:(id)anObject

addObject给集合添加一个对象,如果已有这个对象则不会添加

– (void)addObject:(id)object

removeObject删除集合中给定的对象

– (void)removeObject:(id)anObject

intersectSet取两个集合的交集,如果接收集合中的成员不是给定集合的成员,则从接受集合中删除这个成员。

– (void)intersectSet:(NSSet *)otherSet

unionSet取两个集合的并集,如果给定集合中的成员不是接收集合的成员,则将这个成员添加到接收集合中。

– (void)unionSet:(NSSet *)otherSet

三、NSSet的全部方法

Creating a Set

+ set

+ setWithArray:

+ setWithObject:

+ setWithObjects:

+ setWithObjects:count:

+ setWithSet:

– setByAddingObject:

– setByAddingObjectsFromSet:

– setByAddingObjectsFromArray:

Initializing a Set

– initWithArray:

– initWithObjects:

– initWithObjects:count:

– initWithSet:

– initWithSet:copyItems:

Counting Entries

– count

Accessing Set Members

– allObjects

– anyObject

– containsObject:

– filteredSetUsingPredicate:

– makeObjectsPerformSelector:

– makeObjectsPerformSelector:withObject:

– member:

– objectEnumerator

– enumerateObjectsUsingBlock:

– enumerateObjectsWithOptions:usingBlock:

– objectsPassingTest:

– objectsWithOptions:passingTest:

Comparing Sets

– isSubsetOfSet:

– intersectsSet:

– isEqualToSet:

– valueForKey:

– setValue:forKey:

Creating a Sorted Array

– sortedArrayUsingDescriptors:

Key-Value Observing

– addObserver:forKeyPath:options:context:

– removeObserver:forKeyPath:context:

– removeObserver:forKeyPath:

Describing a Set

– description

– descriptionWithLocale:

四、NSMutableSet的全部方法

Creating a Mutable Set

+ setWithCapacity:

– initWithCapacity:

Adding and Removing Entries

– addObject:

– filterUsingPredicate:

– removeObject:

– removeAllObjects

– addObjectsFromArray:

Combining and Recombining Sets

– unionSet:

– minusSet:

– intersectSet:

– setSet:
本文转载地址:http://blog.it985.com/15878.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: