• NSSet、NSMutableSet、NSOrderedSet、NSMutableOrderedSet


    NSSet、NSMutableSet是无序的,但是它可以保证数据的唯一性。当插入相同数据时,不会有任何的效果。从内部的实现来说是hash表,所以可以常数时间内查找到一个数据。

    NSOrderedSet、NSMutableOrderedSet是有序的。

        NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"8",@"6",@"7",@"4", nil];
        NSLog(@"%@", set);
        // 获取set中的数据
        NSArray *arr1 = [set allObjects];
        NSLog(@"%@", arr1);
    
    // 打印结果:
    2017-02-28 15:53:44.359 UsingWebView[83398:10002574] {(
        7,
        3,
        8,
        4,
        9,
        5,
        1,
        6,
        2
    )}
    2017-02-28 15:53:44.360 UsingWebView[83398:10002574] (
        7,
        3,
        8,
        4,
        9,
        5,
        1,
        6,
        2
    )
    NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];
    
        NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] init];
        for (NSString *value in array) {
            [mutableOrderSet addObject:value];
        }
        NSLog(@"%@", mutableOrderSet);
        
        NSEnumerator *enumerator = [mutableOrderSet objectEnumerator];
        for (NSString *value in enumerator) {
            NSLog(@"%@", value);
        }
        
        NSArray *arr2 = [mutableOrderSet array];
        NSLog(@"%@",arr2);
        
        NSSet *set = [mutableOrderSet set];
        NSLog(@"%@", set);
    
    // 打印结果:
    2017-02-28 15:57:17.184 UsingWebView[83465:10010708] {(
        1,
        9,
        3,
        2,
        5,
        6,
        4
    )}
    2017-02-28 15:57:17.186 UsingWebView[83465:10010708] 1
    2017-02-28 15:57:17.186 UsingWebView[83465:10010708] 9
    2017-02-28 15:57:17.187 UsingWebView[83465:10010708] 3
    2017-02-28 15:57:17.188 UsingWebView[83465:10010708] 2
    2017-02-28 15:57:17.189 UsingWebView[83465:10010708] 5
    2017-02-28 15:57:17.192 UsingWebView[83465:10010708] 6
    2017-02-28 15:57:17.194 UsingWebView[83465:10010708] 4
    2017-02-28 15:57:17.194 UsingWebView[83465:10010708] (
        1,
        9,
        3,
        2,
        5,
        6,
        4
    )
    2017-02-28 15:57:17.195 UsingWebView[83465:10010708] {(
        1,
        9,
        3,
        2,
        5,
        6,
        4
    )}

      NSMutableSet、NSMutableOrderedSet中的三个方法

    // 两个集合的交集
    - (void)intersectSet:(NSSet<ObjectType> *)other;
    //  向集合中删除other集合中的所有元素
    - (void)minusSet:(NSSet<ObjectType> *)other;
    //  向集合中添加other集合中的所有元素 
    - (void)unionSet:(NSSet<ObjectType> *)other;
        NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];
        NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9",@"3",@"0", nil];
        
        [mutableOrderSet intersectSet:set];    
        NSLog(@"%@", mutableOrderSet);
    
    // 返回结果
    2017-02-28 16:06:10.558 UsingWebView[83631:10032695] {(
        1,
        9,
        3
    )}
        NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];
        NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9",@"3",@"0", nil];
        
        [mutableOrderSet minusSet:set];    
        NSLog(@"%@", mutableOrderSet);
    
    //返回结果
    2017-02-28 16:07:52.490 UsingWebView[83671:10036861] {(
        2,
        5,
        6,
        4
    )}
        NSMutableOrderedSet *mutableOrderSet = [[NSMutableOrderedSet alloc] initWithObjects:@"1",@"9", @"3",@"2",@"5",@"2",@"6",@"9",@"4", nil];
        NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"9",@"3",@"0", nil];
        
        [mutableOrderSet unionSet:set];
        NSLog(@"%@", mutableOrderSet);
    
    // 返回结果
    2017-02-28 16:08:44.067 UsingWebView[83700:10039896] {(
        1,
        9,
        3,
        2,
        5,
        6,
        4,
        0
    )}

     

  • 相关阅读:
    CHROME下去掉保存密码后输入框变成黄色背景样式
    AJAX请求遭遇未登录和Session失效的解决方案
    Oracle数据导入导出imp/exp
    缓存技术
    存储过程中引用的常规表,临时表以及表变量是否会导致存储过程的重编译
    给定一张表(列有月份,销售额),要求查询出月份、本月销售额、上月销售额这三个结果,如果当月上个月的销售额不存在就显示为“*”。
    tempdb 数据文件暴涨
    数据库还原成功之后,数据库依然处于还原状态
    CONVERT时间
    sql 2008 链接服务器到 sql 2000
  • 原文地址:https://www.cnblogs.com/muzijie/p/6479342.html
Copyright © 2020-2023  润新知