程式碼:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化NSSet
NSSet *set1=[NSSet set];
NSSet *set2=[NSSet setWithObjects:@"jack",@"yang",nil];
//獲取NSSet中資料的個數
NSInteger count=[set2 count];
//隨機取出NSSet中的元素
NSString *str=[set2 anyObject];
//通過陣列建立集合
NSArray *arr=[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"a",nil];
NSSet *set3=[NSSet setWithArray:arr];
//判斷集合中是否包含
BOOL bo1=[set3 containsObject:@"b"];
//判斷兩個集合是否含有相同元素
BOOL bo2=[set2 intersectsSet:set3];
//集合1是否是集合2的子集
BOOL bo3=[set1 isSubsetOfSet:set2];
//NSMutableSet
NSMutableSet *mSet1=[NSMutableSet setWithObjects:@"",@"",@"",@"", nil];
NSMutableSet *mSet2=[NSMutableSet setWithObjects:@"a",@"", nil];
NSMutableSet *mSet3=[NSMutableSet set];
//集合1減去集合2
//[mSet1 minusSet:mSet2];
//集合1與集合2交集(mSet1元素改變,mSet2不變)
//[mSet1 intersectSet:mSet2];
//集合1與集合2並集(mSet1元素改變,mSet2不變)
[mSet1 unionSet:mSet2];
//移除集合中的元素
[mSet1 removeObject:@""];
}
return ;
}