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
)}

 

 

转载于:https://www.cnblogs.com/muzijie/p/6479342.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值