OC基础:NSSet

NSSet 提供了一个可以用于无序对象集合的类。通过NSSet,你可以存储不需要按一定顺序存储的对象。
NSSet 的使用总结如下:
1.工厂方法创建:

#pragma mark 创建Set
-(void)Creating
{
    // 空set
    NSSet *set = [NSSet set];
    
    // 通过数组
    NSArray *array = [NSArray arrayWithObjects:@"Hello",@"World",@"Hello", nil];
    set = [NSSet setWithArray:array];
    
    // 只包含一个元素
    set = [NSSet setWithObject:@"Hello"];
    
    //多个初始化
    set = [NSSet setWithObjects:@"Hello",@"World",nil];
    
    //通过NSSet
    set = [NSSet setWithSet:set];
    
    // 通过NSSet增加一个元素
    NSSet *set1 = [set setByAddingObject:@"iOS"];
    
    // 通过NSSet增加元素
    NSSet *set2 = [set setByAddingObjectsFromSet:set1];
    
     // 通过NSArray增加元素
    set2 = [set setByAddingObjectsFromArray:array];
    
}

2.初始化方法创建NSSet

#pragma mark 初始化Set
-(void)Init
{
    NSArray *array = [NSArray arrayWithObjects:@"Hello",@"World",@"Hello", nil];
    
    //空set
    NSSet *set = [[NSSet alloc]init];
    
    // 通过数组
    set = [[NSSet alloc] initWithArray:array];
    
    // 多个初始化
    set = [[NSSet alloc] initWithObjects:@"Hello", @"World", nil];
    
    // 通过set
    set = [[NSSet alloc] initWithSet:set];
    set = [[NSSet alloc] initWithSet:set copyItems:YES];
}

3.计算数量

#pragma mark 计算条目
-(void)CountingEntries
{
    NSSet *set = [NSSet setWithObjects:@"Hello",@"World",@"Hello",nil];
    NSLog(@"count:%lu", (unsigned long)set.count);
}

4.访问Set成员

#pragma mark 访问Set成员
-(void)AccessingSetMembers
{
    NSSet *set = [NSSet setWithObjects:@"Hello", @"World", nil];
    // 所有成员
    NSArray *array = set.allObjects;
    NSLog(@"allObjects:%@", array);

    // 随机获取
    NSString *anyObject = [set anyObject];
    
    // 是否存在某个对象
    BOOL containsObject = [set containsObject:anyObject];
    NSLog(@"containsObject:%d", containsObject);

    // 通过过滤器过滤
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary * _Nullable bindings) {
        return YES;
    }];
    set = [set filteredSetUsingPredicate:predicate];
    
    // block 过滤
    set = [set objectsPassingTest:^BOOL(NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        return YES;
    }];
    
    // block 过滤, 并设置并发模式
    set = [set objectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        return YES;
    }];
    
    // 给每个成员发送消息
    [set makeObjectsPerformSelector:@selector(length)];
    
    // 给每个成员发送消息并携带数据
    [set makeObjectsPerformSelector:@selector(hasPrefix:) withObject:@"iOS"];
    
    // 当存在这个对象时,返回这个对象
    NSString *member = @"iOS";
    member = [set member:member];
    
    // forIn 遍历
    for (id value in set) {
        NSLog(@"%@", value);
    }
    
    // enum遍历
    NSEnumerator *enumerator = [set objectEnumerator];
    id value;
    while (value = [enumerator nextObject]) {
        /* code that acts on the set’s values */
        NSLog(@"%@", value);
    }
    
    // block 遍历
    [set enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        
    }];
    
    // block 遍历, 并设置并发模式
    [set enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSString * _Nonnull obj, BOOL * _Nonnull stop) {
        
    }];

}

5.NSSet 比较

#pragma mark 比较Set
-(void)ComparingSets
{
    NSSet *set = [NSSet setWithObjects:@"Hello", @"World", nil];
    NSSet *set2 = [NSSet setWithObjects:@"Hello", @"World",@"iOS", nil];
    
    // 是否子集合
    BOOL isBool = [set isSubsetOfSet:set2];
    
    // 是否有交集
    isBool = [set intersectsSet:set2];
    
    // 是否相等
    isBool = [set isEqualToSet:set2];

}

6.排序

-(void)Sorting
{
    // 降序排列
    NSSet *set = [NSSet setWithObjects:@"1", @"3", @"2", nil];
    NSSortDescriptor *sortDes = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO];
    NSArray *sArray = [NSArray arrayWithObjects:sortDes, nil];
    NSArray *array = [set sortedArrayUsingDescriptors:sArray];
    NSLog(@"%@", array);
}

7.NSMutableSet 创建

-(void)Creating
{
    // 创建可变集合,并设置初始的内部元素个数
    NSMutableSet *mSet = [NSMutableSet setWithCapacity:10];
    mSet = [[NSMutableSet alloc] initWithCapacity:10];
}

8.NSMutableSet 添加和删除元素

-(void)AddingAndRemoving
{
    NSMutableSet *mSet = [NSMutableSet set];
    
    //增加
    [mSet addObject:@"Hello"];
    
    // 同时增加多个对象
    [mSet addObjectsFromArray:[NSArray arrayWithObjects:@"Hello",@"World", nil]];
    
    // 过滤
    NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
    [mSet filterUsingPredicate:predicate];
    
    // 删除单一对象
    [mSet removeObject:@"iOS"];
    
    // 删除全部
    [mSet removeAllObjects];
}

9.NSMutableSet 结合和重组

#pragma mark 结合和重组
-(void)CombingAndRecombining
{
    NSMutableSet *mSet = [NSMutableSet setWithObjects:@"Hello",@"World", nil];
    NSMutableSet *mSet2 = [NSMutableSet setWithObjects:@"Hello",@"iOS", nil];
    
    // 并集
    [mSet unionSet:mSet2];
    
    // 差集
    [mSet minusSet:mSet2];
    
    // 交集
    [mSet intersectSet:mSet2];
    
    // 全替换
    [mSet setSet:mSet2];  
}

demo地址:demo

你可能感兴趣的:(OC基础:NSSet)