zoukankan      html  css  js  c++  java
  • 常用基础OC 集合

     //    2016071917:50:53    集合

            

            

            //七、NSSet 集合对象(容器类,)

            

            

            //  1. 使用类方法创建对象

            

            NSSet *set1 = [NSSet set];  //  创建一个空的集合对象

            

            NSSet *set2 = [NSSet setWithObject:@"abc"];

            

            NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];

            

            NSLog(@"%@", set3);

            

            

            

            NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];

            

            NSSet *set4 = [NSSet setWithArray:array];   //  使用数组创建

            

            NSLog(@"%@", set4);

            

            

            

            NSSet *set5 = [NSSet setWithSet:set4];      //  使用集合创建

            

            NSLog(@"%@", set5);

            

            

            

            //  2.使用实例方法创建

            

            NSSet *set6 = [[NSSet alloc] init];

            

            NSLog(@"%@", set6);

            

            NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];

            

            NSLog(@"%@", set7);

            

            NSSet *set8 = [[NSSet alloc] initWithArray:array];

            

            NSLog(@"%@", set8);

            

            NSSet *set9 = [[NSSet alloc] initWithSet:set7];

            

            NSLog(@"%@", set9);

            

            

            

            //  3.返回几个元素个数

            

            NSLog(@"%ld", [set7 count]);

            

            

            

            //  4.枚举器访问集合元素

            

            NSEnumerator *enumerator = [set7 objectEnumerator];

            

            NSString *str = nil;

            

            while (str = [enumerator nextObject]) {

                

                NSLog(@"%@", str);

                

            }

            

            

            

            //  5.判断两个几个是否有交集

            

            BOOL ifhasIntersection = [set2 intersectsSet:set3];

            

            NSLog(@"%d", ifhasIntersection);

            

            

            

            //  6.判断两个集合是否相等

            

            NSLog(@"%d", [set2 isEqualToSet:set3]);

            

            

            

            //  7.判断当前集合是否是子集

            

            NSLog(@"%d", [set2 isSubsetOfSet:set3]);

            

            

            

            //5.2可变集合 NSMutableSet

            

            //  创建指定元素个数的一个集合对象

            

            NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];

            

            [mutableSet addObject:@"aaa"];

            

            NSLog(@"%@", mutableSet);

            

            //  类方法创建可变集合

            

            NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];

            

            NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];

            

            

            

            //  添加一个对象到集合

            

            [mutableSet2 addObject:@"ddd"];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  从集合中删除一个对象

            

            [mutableSet2 removeObject:@"ddd"];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  把数组对象添加到集合对象中

            

            NSArray *arr10 = [NSArray arrayWithObjects:@"eee", @"fff", nil];

            

            [mutableSet1 addObjectsFromArray:arr10];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  得到两个集合的交集 注意intersectSetintersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool

            

            [mutableSet1 intersectSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  从一个集合中减去另一个集合

            

            [mutableSet1 minusSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  从一个元素中删除所有元素

            

            [mutableSet2 removeAllObjects];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  取两个集合的并集

            

            [mutableSet1 unionSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  设置给集合赋值

            

            [mutableSet1 setSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

  • 相关阅读:
    多线程循环打印ABC
    程序员如何提高影响力
    一文详解bundle adjustment
    粒子滤波到底是怎么得到的?
    多视图立体匹配论文分享CasMVSNet
    入坑slam,一位博士小姐姐的科研和成长分享(考研+读研+读博)
    【车道线检测】一种基于神经网络+结构约束的车道线检测方法
    FCGF-基于稀疏全卷积网络的点云特征描述子提取(ICCV2019)
    多视图立体匹配论文分享PVA-MVSNet
    姿态估计算法汇总|基于RGB、RGB-D以及点云数据
  • 原文地址:https://www.cnblogs.com/DafaRan/p/5700727.html
Copyright © 2011-2022 走看看