Swift如何从IndexSetB中移除IndexSetA?

How to remove IndexSetA from IndexSetB in Swift?

在Objective-C中,NSIndexSet可以removeIndexes:(NSIndexSet *)indexSet:

[aIndexSet removeIndexes: bIndexSet];

有没有什么方法可以像 Objective-C 中的 NSIndexSet 那样在 Swift 中使用 IndexSet 来做到这一点?

有个函数,来自documentation

mutating func subtract(_ other: IndexSet)

Removes the elements of the given set from this set.


var aIndexSet = IndexSet(integersIn: 0..<7)
let bIndexSet = IndexSet(integersIn: 4..<12)

aIndexSet.subtract(bIndexSet)

print(aIndexSet) // 0..<3