NSSet 集合

前言

  • NSSet:集合

    • Objective-C

      1
      2
      @interface NSSet<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
      @interface NSMutableSet<ObjectType> : NSSet<ObjectType>
    • Swift

      1
      public class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration
    • NSSet 集合跟数组差不多,但 Set 集合不能存放相同的对象,它是一组单值对象的集合,被存放进集合中的数据是无序的,它可以是可变的,也可以是不变的。

    • Xcode 7 对系统中常用的一系列容器类型都增加了泛型支持(),有了泛型后就可以指定容器类中对象的类型了。假如向泛型容器中加入错误的对象,编译器会报警告。

      1
      2
      __covariant:协变性,子类型可以强转到父类型(里氏替换原则)。
      __contravariant:逆变性,父类型可以强转到子类型。
    • 不指定泛型类型的对象可以和任意泛型类型转化,但指定了泛型类型后,两个不同类型间是不可以强转的,假如你希望主动控制转化关系,就需要使用泛型的协变性和逆变性修饰符。

  • Set:集合

    • Swift

      1
      public struct Set<Element : Hashable> : Hashable, CollectionType, ArrayLiteralConvertible
    • 集 NSSet 是一个无序对象的集合。集也有动态和静态之分,NSSet 是静态类型,成员不能被修改。NSMutableSet 是动态类型,成员可以被修改。

    • 集和数组不一样的是,它存储的每一个元素都是唯一的,没有完全相同的两个元素。所以数组中如果有两个相同的值,转换为集后只会有一个值。

  • NSCountedSet:计数集合

    • Objective-C

      1
      @interface NSCountedSet<ObjectType> : NSMutableSet<ObjectType>
    • Swift

      1
      public class NSCountedSet : NSMutableSet
    • NSCountedSet 这种集合中同一对象可以出现多次,然而并非在集合中存放多次这个对象,而是维护一个次数计数。当第一次将对象添加到集合中时,对象的 count 值被置为 1,然后每次将这个对象添加到集合中, count 值就会增加 1,每次从集合中删除对象,count 值就会减 1。当对象的 count 值为零时,实际上对象本身就被删除了。

  • NSIndexSet:索引集合

    • Objective-C

      1
      2
      @interface NSIndexSet : NSObject <NSCopying, NSMutableCopying, NSSecureCoding>
      @interface NSMutableIndexSet : NSIndexSet
    • Swift

      1
      2
      public class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding
      public class NSMutableIndexSet : NSIndexSet
    • NSIndexSet 这种集合用于存储有序的索引到某种数据结构,比如数组。

1、NSSet 不可变集合的创建

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    // 对象方法创建

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

    // 与 set1 的地址相同
    NSSet *set2 = [[NSSet alloc] initWithSet:set1];

    NSSet *set3 = [[NSSet alloc] initWithObjects:@55, @23, @3, @8, nil];
    NSSet *set4 = [[NSSet alloc] initWithArray:@[@55, @23, @3, @8, @21, @33, @18]];

    // 类方法创建

    NSSet *set5 = [NSSet set];

    // 与 set1 的地址相同
    NSSet *set6 = [NSSet setWithSet:set1];

    NSSet *set7 = [NSSet setWithObject:@8];
    NSSet *set8 = [NSSet setWithObjects:@55, @23, @3, @8, nil];
    NSSet *set9 = [NSSet setWithArray:@[@55, @23, @3, @8, @21, @33, @18]];

    // 已有对象创建

    NSSet *set10 = [set1 setByAddingObject:@8];
    NSSet *set11 = [set1 setByAddingObjectsFromSet:set9];
    NSSet *set12 = [set1 setByAddingObjectsFromArray:@[@55, @23, @3, @8, @21, @33, @18]];

    // 泛型方法

    NSSet<NSNumber *> *set13 = [NSSet setWithObjects:@55, @23, @3, @8, nil];

2、NSSet/Set 集合成员个数的计算

  • Objective-C

    1
    2
    3
    NSSet *set = [NSSet setWithObjects:@55, @23, @3, @8, @21, @33, @18, nil];

    NSUInteger length = [set count];
  • Swift

    1
    2
    3
    4
    5
    // Set 或 NSSet 型集合

    let set: NSSet = [1, 3, 5, 7]

    let length: Int = set.count

3、NSSet 集合元素的获取

  • Objective-C

    1
    2
    3
    4
    NSSet *set = [NSSet setWithObjects:@55, @23, @3, @8, @21, @33, @18, nil];

    // 获取集合中的任意元素
    id anyObject = [set anyObject];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    let nsSet: NSSet = [2, 4, 6, 8]

    // 获取集合中指定的元素,不存在时返回 nil
    let object1: AnyObject? = nsSet.member(8)

    // 获取集合中随机的一个值,集合为空时返回 nil
    let objects1: AnyObject? = nsSet.anyObject()

    // 获取集合中所有的元素
    let objects2: AnyObject = nsSet.allObjects

4、NSSet/Set 判断集合中是否包含某个元素

4.1 NSSet 判断集合中是否包含某个元素

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    NSSet *set = [NSSet setWithObjects:@55, @23, @3, @8, @21, @33, @18, nil];

    // containsObject

    // 判断是否包含某个元素,包含返回 1, 不包含返回 0
    BOOL containsObject = [set containsObject:@3];

    // member

    // 判断是否包含某个元素,包含返回元素值, 不包含返回 null
    id member = [set member:@8];
  • Swift

    1
    2
    3
    4
    let nsSet1: NSSet = [2, 4, 6, 8]

    // 判断集合中是否包含某个元素
    let bl5: Bool = nsSet1.containsObject(8)

4.2 Set 判断集合中是否包含某个元素

  • Swift

    1
    2
    3
    4
    let swiftSet1: Set = [1, 3, 5, 7]

    // 判断集合中是否包含某个元素
    let bl2: Bool = swiftSet1.contains(7)

5、NSSet/Set 集合的比较

5.1 NSSet 集合的比较

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    NSSet *set1 = [NSSet setWithObjects:@55, @23, @3, @8, @21, @33, @18, nil];
    NSSet *set2 = [NSSet setWithObjects:@5, @23, @3, @8, @22, @33, @81, nil];

    // isEqualToSet

    // 判断两个集合是否相等
    BOOL isEqualToSet = [set1 isEqualToSet:set2];

    // isSubsetOfSet

    // 判断一个集合是否为另一个集合的子集
    BOOL isSubsetOfSet = [set1 isSubsetOfSet:set2];

    // intersectsSet

    // 判断一个集合和另一个集合是否有交集
    BOOL intersectsSet = [set1 intersectsSet:set2];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    let nsSet1: NSSet = [2, 4, 6, 8]
    let nsSet2: NSSet = [6, 8, 10, 12]
    let nsSet3: NSMutableSet = [6, 8, 10, 12]

    // 相等,判断两个集合是否相等
    let bl6: Bool = nsSet1 == nsSet2

    // 相等,判断两个集合是否相等
    let bl7: Bool = nsSet1.isEqualToSet(nsSet2 as Set)

    // 子集,判断当前集合是否是指定集合的子集
    let bl8: Bool = nsSet1.isSubsetOfSet(nsSet2 as Set)

    // 交集,判断当前集合与指定集合是否有交集
    let bl9: Bool = nsSet1.intersectsSet(nsSet2 as Set)

    // 交集,返回当前集合与指定集合的交集,返回到当前集合中
    nsSet3.intersectSet(nsSet1 as Set)

    // 并集,返回当前集合与指定集合的并集,返回到当前集合中
    nsSet3.unionSet(nsSet1 as Set)

    // 补集,返回当前集合与指定集合的补集,返回到当前集合中,当前集合中删除与指定集合的交集
    nsSet3.minusSet(nsSet1 as Set)

5.2 Set 集合的比较

  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    let swiftSet1: Set = [1, 3, 5, 7]
    let swiftSet2: Set = [5, 7, 9, 11]

    // 判断集合是否为空
    let bl1: Bool = swiftSet1.isEmpty

    // 相等,判断两个集合是否相等
    let bl3: Bool = swiftSet1 == swiftSet2

    // 子集,判断当前集合是否是指定集合的子集
    let bl4: Bool = swiftSet1.isSubsetOf(swiftSet2)

    // 交集,返回当前集合与指定集合的交集,没有交集时返回一个空集合
    let interSet: Set = swiftSet1.intersect(swiftSet2)

    // 并集,返回当前集合与指定集合的并集
    let unionSet: Set = swiftSet1.union(swiftSet2)

6、NSSet/Set 可变集合的创建

6.1 NSSet 可变集合的创建

  • Objective-C

    1
    2
    3
    4
    5
    // 对象方法创建
    NSMutableSet *set1 = [[NSMutableSet alloc] initWithCapacity:0];

    // 类方法创建
    NSMutableSet *set2 = [NSMutableSet setWithCapacity:0];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    let swiftArr:Array = ["a", "b", "c"]
    let swiftSet:Set = [1, 2, 3, 4]

    // 创建一个空集合
    let nsSet1:NSSet = NSSet()

    // 指定多个元素值
    let nsSet2:NSSet = [2, 4, 6, 8]

    // 由 Array 型数组创建
    let nsSet3:NSSet = NSSet(array:swiftArr)

    // 由 Set 型集合创建
    let nsSet4:NSSet = NSSet(set: swiftSet)

    // 由 NSSet 型集合创建
    let nsSet5:NSSet = NSSet(set: nsSet3)

    // 指定一个元素值
    let nsSet6:NSSet = NSSet(object: 7)

    // 指定多个元素值
    let nsSet7:NSSet = NSSet(objects: 2, 4, 6, 8)

6.2 Set 集合的创建

  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    // 不赋初值,指定数据类型为 String 型
    let set1 = Set<String>()

    // 不指定数据类型,自动推断
    let set2: Set = [1, 3, 5, 7]

    // 指定数据类型为 Int 型
    let set3: Set<Int> = [1, 3, 5, 7]

7、NSMutableSet/Set 集合元素的添加

7.1 NSMutableSet 集合元素的添加

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    NSMutableSet *set = [NSMutableSet setWithCapacity:0];

    // addObject
    [set addObject:@8];

    // addObjectsFromArray
    [set addObjectsFromArray:@[@5, @23, @3, @21]];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    let nsSet: NSMutableSet = [2, 4, 6, 8]

    // 向集合中添加一个元素
    nsSet.addObject(10)

    // 向集合中添加一个数组
    nsSet.addObjectsFromArray([11, 12, 12])

7.2 Set 集合元素的添加

  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
        var swiftSet: Set = [1, 3, 5, 7]

    // 向集合中添加一个元素
    swiftSet.insert(8)
    ```

    # 8NSMutableSet/Set 集合元素的删除

    ## 8.1 NSMutableSet 集合元素的删除

    - Objective-C

    ```objc
    NSMutableSet *set = [NSMutableSet setWithObjects:@5, @23, @3, @8, @21, @33, @18, nil];

    // removeObject

    // 删除指定元素
    [set removeObject:@8];

    // minusSet

    // 删除指定集合中有的所有元素,当前集合中删除与指定集合的交集
    NSSet *set1 = [NSSet setWithObjects:@23, @18, @33, nil];
    [set minusSet:set1];

    // removeAllObjects

    // 删除所有元素
    [set removeAllObjects];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    let nsSet: NSMutableSet = [2, 4, 6, 8]

    // 删除指定元素
    nsSet.removeObject(4)

    // 删除指定集合,当前集合中删除与指定集合的交集
    nsSet.minusSet([2, 6])

    // 删除所有元素
    nsSet.removeAllObjects()

8.2 Set 集合元素的删除

  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
        var swiftSet: Set = [1, 3, 5, 7]

    // 删除第一个元素
    swiftSet.removeFirst()

    // 删除指定元素
    swiftSet.remove(1)

    // 删除所有元素
    swiftSet.removeAll(keepCapacity: true)
    ```

    # 9NSMutableSet 集合元素的修改

    - Objective-C

    ```objc
    NSMutableSet *set = [NSMutableSet setWithObjects:@5, @23, @3, @8, @21, @33, @18, nil];

    // 覆盖所有元素
    [set setSet: [NSSet setWithObjects:@2, @3, nil]];
  • Swift

    1
    2
    3
    let nsSet: NSMutableSet = [2, 4, 6, 8]

    nsSet.setSet([1, 2, 3])

10、NSMutableSet 取两个集合的交集

  • Objective-C

    1
    2
    3
    4
    5
    NSMutableSet *set = [NSMutableSet setWithObjects:@55, @23, @3, @8, @21, @33, @18, nil];
    NSMutableSet *set1 = [NSMutableSet setWithObjects:@5, @23, @3, @8, @22, @33, @81, nil];

    // 交集保存到接收集合中
    [set intersectSet: set1];

11、NSMutableSet 取两个集合的并集

  • Objective-C

    1
    2
    3
    4
    5
    NSMutableSet *set = [NSMutableSet setWithObjects:@55, @23, @3, @8, @21, @33, @18, nil];
    NSMutableSet *set1 = [NSMutableSet setWithObjects:@5, @23, @3, @8, @22, @33, @81, nil];

    // 并集保存到接收集合中
    [set unionSet: set1];

12、NSSet 集合的条件遍历

  • 用 Block(闭包)遍历

    • Objective-C

      1
      2
      3
      4
      5
      NSSet *set = [NSSet setWithObjects:@5, @23, @3, @8, @21, @33, @18, nil];

      [set enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
      NSLog(@"%@", obj);
      }];
    • Swift

      1
      2
      3
      4
      5
      6
      let nsSet: NSSet = [2, 4, 6, 8]

      nsSet.enumerateObjectsUsingBlock { (obj: AnyObject, stop: UnsafeMutablePointer<ObjCBool>) in

      print(obj)
      }
  • 用 迭代 遍历

    • Objective-C

      1
      2
      3
      4
      5
      6
      7
      8
      NSSet *set = [NSSet setWithObjects:@5, @23, @3, @8, @21, @33, @18, nil];

      NSEnumerator *enu = [set objectEnumerator];

      id obj = nil;
      while (obj = [enu nextObject]) {
      NSLog(@"%@", obj);
      }
    • Swift

      1
      2
      3
      4
      5
      6
      7
      8
      9
      let nsSet: NSSet = [2, 4, 6, 8]

      let enu: NSEnumerator = nsSet.objectEnumerator()

      // AnyObject 后不要加 ?,否则会导致一直循环
      while let obj: AnyObject = enu.nextObject() {

      print(obj)
      }
  • 条件遍历

    • Objective-C

      1
      2
      3
      4
      5
      6
      7
      NSSet *set = [NSSet setWithObjects:@5, @23, @3, @8, @21, @33, @18, nil];

      // 遍历集合,取出所有满足条件(能返回 YES)的值
      NSSet *set1 = [set objectsPassingTest:^BOOL(id _Nonnull obj, BOOL * _Nonnull stop) {

      return [obj intValue] > 10 ? : NO;
      }];
    • Swift

      1
      2
      3
      4
      5
      6
      7
      8
      let nsSet:NSSet = [2, 4, 6, 8]

      let set: Set = nsSet.objectsPassingTest { (obj: AnyObject, stop: UnsafeMutablePointer<ObjCBool>) -> Bool in

      return (obj as! Int) > 5 ? true : false
      }

      print(set)

13、计数集合 NSCountedSet 的创建

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    NSCountedSet *countedSet1 = [[NSCountedSet alloc] init];
    NSCountedSet *countedSet2 = [[NSCountedSet alloc] initWithSet:countedSet1];
    NSCountedSet *countedSet3 = [[NSCountedSet alloc] initWithCapacity:0];
    NSCountedSet *countedSet4 = [[NSCountedSet alloc] initWithArray:@[@5, @23, @3, @8, @22, @33, @81]];

    // 输出时每个元素后面的中括号中显示该元素值在集合中的个数
    NSLog(@"%@", countedSet4);
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    let nsCountSet1: NSCountedSet = NSCountedSet()
    let nsCountSet2: NSCountedSet = NSCountedSet(capacity: 0)

    let nsCountSet3: NSCountedSet = NSCountedSet(array: [1, 3, 5, 7, 9])

    let nsCountSet4: NSCountedSet = NSCountedSet(set: [6, 9])

    let nsCountSet5: NSCountedSet = NSCountedSet(object: 8)
    let nsCountSet6: NSCountedSet = NSCountedSet(objects: [2, 3, 6, 8], count: 4)

14、计数集合元素的添加

  • Objective-C

    1
    2
    3
    4
    5
    6
    NSCountedSet *set = [NSCountedSet setWithObjects:@2, @3, @4, nil];

    // 向集合中多次添加同一个元素,该元素的计数值增加
    [set addObject:@4];
    [set addObject:@4];
    [set addObject:@4];
  • Swift

    1
    2
    3
    4
    let nsCountSet: NSCountedSet = [1, 3, 5, 7, 9]

    // 向集合中多次添加同一个元素,该元素的计数值增加
    nsCountSet.addObject(6)

15、计数集合元素的删除

  • Objective-C

    1
    2
    3
    4
    5
    NSCountedSet *set = [NSCountedSet setWithObjects:@2, @3, @4, nil];
    [set addObject:@4];

    // 从集合中删除元素,该元素的计数值减小
    [set removeObject:@4];
  • Swift

    1
    2
    3
    4
    let nsCountSet: NSCountedSet = [1, 3, 5, 7, 9]

    // 从集合中删除元素,该元素的计数值减小
    nsCountSet.removeObject(5)

16、计数集合计数值的计算

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    NSCountedSet *set = [NSCountedSet setWithObjects:@2, @3, @4, nil];
    [set addObject:@4];
    [set addObject:@4];
    [set addObject:@4];

    // 检索集合中某个元素的计数值
    NSUInteger countForObject = [set countForObject:@4];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    let nsCountedSet: NSCountedSet = NSCountedSet(array: [1, 2, 3])

    nsCountedSet.addObject(2)
    nsCountedSet.removeObject(1)

    // 检索集合中指定元素的计数值
    let num1: Int = nsCountedSet.countForObject(1)

    // 检索集合中指定元素的计数值
    let num2: Int = nsCountedSet.countForObject(2)

17、索引集合 NSIndexSet 的创建

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    // 对象方法创建

    // 创建一个空的索引集合
    NSIndexSet *indexSet1 = [[NSIndexSet alloc] init];

    // 与 indexSet1 的地址不同
    NSIndexSet *indexSet2 = [[NSIndexSet alloc] initWithIndexSet:indexSet1];

    // 创建有 1 个元素的索引集合,元素为 8
    NSIndexSet *indexSet3 = [[NSIndexSet alloc] initWithIndex:8];

    // 创建有 5 个元素的索引集合,元素为 2,3,4,5,6
    NSIndexSet *indexSet4 = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(2, 5)];

    // 类方法创建

    // 创建一个空的索引集合
    NSIndexSet *indexSet5 = [NSIndexSet indexSet];

    // 创建有 1 个元素的索引集合,元素为 5
    NSIndexSet *indexSet6 = [NSIndexSet indexSetWithIndex:5];

    // 创建有 8 个元素的索引集合
    NSIndexSet *indexSet7 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 8)];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    let indexSet1: NSIndexSet = NSIndexSet()

    let indexSet2: NSIndexSet = NSIndexSet(index: 8)

    let indexSet3: NSIndexSet = NSIndexSet(indexSet: indexSet2)

    let indexSet4: NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))

18、索引集合元素数量的计算

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 8)];

    // 计算索引集合所有索引的数量
    NSUInteger length1 = [indexSet count];

    // 计算索引集合中某一范围内的索引的数量
    NSUInteger length2 = [indexSet countOfIndexesInRange:NSMakeRange(5, 5)];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    let indexSet: NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))

    // 计算索引集合所有索引的数量
    let num1: Int = indexSet.count

    // 计算索引集合中某一范围内的索引的数量
    let num2: Int = indexSet.countOfIndexesInRange(NSMakeRange(3, 4))

19、索引集合元素的获取

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 8)];

    // firstIndex

    // 获取索引集合中的第一个索引,如果集合为空则返回 NSNotFound
    NSUInteger result1 = [indexSet firstIndex];

    // lastIndex

    // 获取索引集合的最后一个索引,如果集合为空则返回 NSNotFound
    NSUInteger result2 = [indexSet lastIndex];

    // indexLessThanIndex

    // 获取索引集合中小于指定的索引且最接近的索引,如果没有小于的索引则返回 NSNotFound
    NSUInteger result3 = [indexSet indexLessThanIndex:3];

    // indexLessThanOrEqualToIndex

    // 获取索引集合中小于等于指定的索引且最接近的索引,如果没有小于的索引则返回 NSNotFound
    NSUInteger result4 = [indexSet indexLessThanOrEqualToIndex:3];

    // indexGreaterThanIndex

    // 获取索引集合中大于指定的索引且最接近的索引,如果没有大于的索引则返回 NSNotFound
    NSUInteger result5 = [indexSet indexGreaterThanIndex:10];

    // indexGreaterThanIndex

    // 获取索引集合中大于等于指定的索引且最接近的索引,如果没有大于的索引则返回 NSNotFound
    NSUInteger result6 = [indexSet indexGreaterThanOrEqualToIndex:10];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    let indexSet: NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))

    // 获取索引集合中的第一个索引,如果集合为空则返回 NSNotFound
    let index1: Int = indexSet.firstIndex

    // 获取索引集合的最后一个索引,如果集合为空则返回 NSNotFound
    let index2: Int = indexSet.lastIndex

    // 获取索引集合中小于指定的索引且最接近的索引,如果没有小于的索引则返回 NSNotFound
    let index3: Int = indexSet.indexLessThanIndex(5)

    // 获取索引集合中小于等于指定的索引且最接近的索引,如果没有小于的索引则返回 NSNotFound
    let index4: Int = indexSet.indexLessThanOrEqualToIndex(5)

    // 获取索引集合中大于指定的索引且最接近的索引,如果没有大于的索引则返回 NSNotFound
    let index5: Int = indexSet.indexGreaterThanIndex(7)

    // 获取索引集合中大于等于指定的索引且最接近的索引,如果没有大于的索引则返回 NSNotFound
    let index6: Int = indexSet.indexGreaterThanOrEqualToIndex(7)

20、判断索引集合中是否包含某元素

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 5)];

    // containsIndex

    // 判断索引集合中是否包含某个索引
    NSLog(@"%zi", [indexSet containsIndex:7]);

    // containsIndexes

    // 判断一个索引集合中是否包含另一个索引集合
    NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(4, 2)];
    NSLog(@"%zi", [indexSet containsIndexes:indexSet1]);

    // containsIndexesInRange

    // 判断一个索引集合中是否包含某一个范围内的所有值(是否是其父集)
    NSLog(@"%zi", [indexSet containsIndexesInRange:NSMakeRange(4, 3)]);

    // intersectsIndexesInRange

    // 判断一个索引集合中是否包含某一个范围内的值(是否有交集)
    NSLog(@"%zi", [indexSet intersectsIndexesInRange:NSMakeRange(4, 3)]);
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    let indexSet: NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))

    // 判断索引集合中是否包含某个索引
    let bl1: Bool = indexSet.containsIndex(9)

    // 判断一个索引集合中是否包含另一个索引集合
    let bl2: Bool = indexSet.containsIndexes(NSIndexSet(indexesInRange: NSMakeRange(2, 3)))

    // 判断一个索引集合中是否包含某一个范围内的所有值
    let bl3: Bool = indexSet.containsIndexesInRange(NSMakeRange(2, 3))

    // 判断一个索引集合中是否包含某一个范围内的值(是否有交集)
    let bl4: Bool = indexSet.intersectsIndexesInRange(NSMakeRange(7, 3))

21、索引集合元素的比较

  • Objective-C

    1
    2
    3
    4
    NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 5)];
    NSIndexSet *indexSet2 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 4)];

    BOOL isEqual = [indexSet1 isEqualToIndexSet:indexSet2];
  • Swift

    1
    2
    3
    4
    let indexSet: NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))

    // 判断两个索引集合是否相等
    let bl5: Bool = indexSet.isEqualToIndexSet(NSIndexSet(indexesInRange: NSMakeRange(1, 7)))

22、可变索引集合元素的添加

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    NSMutableIndexSet *mIndexSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 5)];

    // addIndex

    // 添加一个索引元素,集合中已存在时会合并
    [mIndexSet addIndex:7];

    // addIndexes

    // 添加一个索引集合,两个索引集合有交集时会合并
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(9, 5)];
    [mIndexSet addIndexes:indexSet];

    // addIndexesInRange

    // 添加一个范围内的元素,有交集时会合并
    [mIndexSet addIndexesInRange:NSMakeRange(15, 3)];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    let mIndexSet: NSMutableIndexSet = NSMutableIndexSet(indexesInRange: NSMakeRange(1, 3))

    // 添加一个索引元素,集合中已存在时会合并
    mIndexSet.addIndex(6)

    // 添加一个索引集合,两个索引集合有交集时会合并
    mIndexSet.addIndexes(NSIndexSet(indexesInRange: NSMakeRange(8, 2)))

    // 添加一个范围内的元素,有交集时会合并
    mIndexSet.addIndexesInRange(NSMakeRange(12, 3))

23、可变索引集合元素的删除

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    NSMutableIndexSet *mIndexSet = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 10)];

    // removeIndex

    // 删除一个索引集合中的索引元素
    [mIndexSet removeIndex:5];

    // removeIndexes

    // 删除指定集合中含有的索引元素
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(7, 3)];
    [mIndexSet removeIndexes:indexSet];

    // removeIndexesInRange

    // 删除指定范围内含有的索引元素
    [mIndexSet removeIndexesInRange:NSMakeRange(2, 2)];

    // removeAllIndexes

    // 删除全部索引元素
    [mIndexSet removeAllIndexes];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    let mIndexSet:NSMutableIndexSet = NSMutableIndexSet(indexesInRange: NSMakeRange(1, 10))

    // 删除一个索引集合中的索引元素
    mIndexSet.removeIndex(2)

    // 删除指定集合中含有的索引元素
    mIndexSet.removeIndexes(NSIndexSet(indexesInRange: NSMakeRange(4, 2)))

    // 删除指定范围内含有的索引元素
    mIndexSet.removeIndexesInRange(NSMakeRange(8, 3))

    // 删除全部索引元素
    mIndexSet.removeAllIndexes()

24、索引集合的遍历

  • 用 Block(闭包)遍历

    • Objective-C

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 10)];

      // enumerateIndexesUsingBlock

      // 遍历获取索引集合的所有索引值
      [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
      NSLog(@"%zi", idx);
      }];

      // enumerateRangesUsingBlock

      // 遍历获取索引集合的起始值和索引元素数量
      [indexSet enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
      NSLog(@"%lu %lu", range.location, range.length);
      }];
    • Swift

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      let indexSet:NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 5))

      // 1. enumerateIndexesUsingBlock

      // 遍历获取索引集合的所有索引值
      indexSet.enumerateIndexesUsingBlock { (idx:Int, stop:UnsafeMutablePointer<ObjCBool>) in

      print(idx)
      }

      // 2. enumerateRangesUsingBlock

      // 遍历获取索引集合的起始值和索引元素数量
      indexSet.enumerateRangesUsingBlock { (range:NSRange, stop:UnsafeMutablePointer<ObjCBool>) in

      print("\(range.location), \(range.length)")
      }
  • 条件遍历

    • Objective-C

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 10)];

      // indexesPassingTest

      NSIndexSet *indexSet1 = [indexSet indexesPassingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {

      // 遍历索引集合,找出索引中所有大于 10 的索引
      return idx > 10 ? : NO;
      }];

      [indexSet1 enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
      NSLog(@"%zi", idx);
      }];

      // indexPassingTest

      NSUInteger index = [indexSet indexPassingTest:^BOOL(NSUInteger idx, BOOL * _Nonnull stop) {

      // 遍历索引集合,找出索引中第一个大于 10 的索引
      return idx > 10 ? : NO;
      }];

      NSLog(@"%zi", index);
    • Swift

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      let indexSet:NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 5))

      // 1. indexesPassingTest

      // 遍历索引集合,找出索引中所有满足条件的索引
      let indexs:NSIndexSet = indexSet.indexesPassingTest { (idx, stop) -> Bool in

      return idx > 3 ? true : false
      }

      indexs.enumerateIndexesUsingBlock { (idx:Int, stop:UnsafeMutablePointer<ObjCBool>) in

      print(idx)
      }

      // 2. indexPassingTest

      // 遍历索引集合,找出索引中第一个满足条件的索引
      let index:Int = indexs.indexPassingTest { (idx, stop) -> Bool in

      return idx > 3 ? true : false
      }

      print(index)
文章目录
  1. 1. 前言
  2. 2. 1、NSSet 不可变集合的创建
  3. 3. 2、NSSet/Set 集合成员个数的计算
  4. 4. 3、NSSet 集合元素的获取
  5. 5. 4、NSSet/Set 判断集合中是否包含某个元素
    1. 5.1. 4.1 NSSet 判断集合中是否包含某个元素
    2. 5.2. 4.2 Set 判断集合中是否包含某个元素
  6. 6. 5、NSSet/Set 集合的比较
    1. 6.1. 5.1 NSSet 集合的比较
    2. 6.2. 5.2 Set 集合的比较
  7. 7. 6、NSSet/Set 可变集合的创建
    1. 7.1. 6.1 NSSet 可变集合的创建
    2. 7.2. 6.2 Set 集合的创建
  8. 8. 7、NSMutableSet/Set 集合元素的添加
    1. 8.1. 7.1 NSMutableSet 集合元素的添加
    2. 8.2. 7.2 Set 集合元素的添加
    3. 8.3. 8.2 Set 集合元素的删除
  9. 9. 10、NSMutableSet 取两个集合的交集
  10. 10. 11、NSMutableSet 取两个集合的并集
  11. 11. 12、NSSet 集合的条件遍历
  12. 12. 13、计数集合 NSCountedSet 的创建
  13. 13. 14、计数集合元素的添加
  14. 14. 15、计数集合元素的删除
  15. 15. 16、计数集合计数值的计算
  16. 16. 17、索引集合 NSIndexSet 的创建
  17. 17. 18、索引集合元素数量的计算
  18. 18. 19、索引集合元素的获取
  19. 19. 20、判断索引集合中是否包含某元素
  20. 20. 21、索引集合元素的比较
  21. 21. 22、可变索引集合元素的添加
  22. 22. 23、可变索引集合元素的删除
  23. 23. 24、索引集合的遍历
隐藏目录