OC之集合的创建及应用

 集合类:存储大量数据数组、字典、set集合

 NSSet

 1.元素是无序的,同一个对象只能保存一个。

 2.都是集合,都能存放多个oc对象,只能是oc对象。

 3.有个可变的子类

 //集合的创建
        NSSet  *set=[NSSet set];
        NSSet *set1=[NSSet setWithObjects:@"jack",@"rose",nil];
        //存取数据的个数
        NSInteger count=[set1 count];
        NSLog(@"%ld",count);
    
        //随机拿取元素
    
        NSString *str=[set1 anyObject];
        NSLog(@"%@",str);
        //通过数组创建集合
        NSArray *arr=[NSArray arrayWithObjects:@"jack",@"rose",@"2", @"9",@"3",nil];
        NSSet *set2=[[NSSet alloc]initWithArray:arr];
        //集合中是否包含内容为“1”这个字符串对象
        BOOL result =[set2 containsObject:@"7"];
        NSLog(@"%d",result);
        //判断两个集合是否含有相同的元素
        BOOL result1=[set1 intersectsSet:set2];
        NSLog(@"%d",result1);

        //集合1是否是集合2的子集合
        BOOL result2=[set1 isSubsetOfSet:set2];
        NSLog(@"%d",result2);
NSMutableSet *set1=[NSMutableSet set];
        NSMutableSet *set2=[NSMutableSet setWithObjects:@"2",@"a",nil];
        NSMutableSet *set3=[NSMutableSet setWithObjects:@"a",@"1",nil];
        
        //集合2减去集合3中元素,最后集合2中的元素只剩1个,值为1;
        [set2 minusSet:set3];
        NSLog(@"%@",set2);
        //集合2与集合3交集,最后集合2中的元素只有1个,值为2
        [set2 intersectSet:set3];
        NSLog(@"%@",set2);
        NSLog(@"%@",set3);
        //集合2与结合3并集,最后集合2中的元素只有3个 1,2,a
        [set2 unionSet:set3];
        NSLog(@"%@",set2);
        NSLog(@"%@",set3);
        [set2 removeObject:@"1"];
        NSLog(@"%@",set2);
        //用集合给集合赋值
        [set1 setSet:set2];
        NSLog(@"%@",set1);

 

转载于:https://www.cnblogs.com/jiangyicheng/p/5121552.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值