集合

#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:@"1",@"2",@"3",@"4", nil];
        NSMutableSet *mSet2=[NSMutableSet setWithObjects:@"a",@"2", 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:@"2"];
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hezhuangzhuang/p/5121103.html