diff --git a/Sources/Rearrange/IndexSet+NSRange.swift b/Sources/Rearrange/IndexSet+NSRange.swift index 5e2c35a..154441d 100644 --- a/Sources/Rearrange/IndexSet+NSRange.swift +++ b/Sources/Rearrange/IndexSet+NSRange.swift @@ -13,12 +13,20 @@ public extension IndexSet { self.init(integersIn: Range(range)!) } + init(ranges: [NSRange]) { + self.init() + + insert(ranges: ranges) + } + mutating func insert(range: NSRange) { insert(integersIn: Range(range)!) } mutating func insert(ranges: [NSRange]) { - ranges.forEach { insert(range: $0) } + for range in ranges { + insert(range: range) + } } mutating func remove(integersIn range: NSRange) { diff --git a/Tests/RearrangeTests/IndexSetExtenstionTests.swift b/Tests/RearrangeTests/IndexSetExtenstionTests.swift index c100c16..d701e69 100644 --- a/Tests/RearrangeTests/IndexSetExtenstionTests.swift +++ b/Tests/RearrangeTests/IndexSetExtenstionTests.swift @@ -8,7 +8,7 @@ import XCTest -class IndexSetExtenstionTests: XCTestCase { +final class IndexSetExtenstionTests: XCTestCase { func testSpanningRange() { var set = IndexSet() @@ -43,6 +43,13 @@ class IndexSetExtenstionTests: XCTestCase { XCTAssertEqual(set, IndexSet(integersIn: NSRange(location: 0, length: 5))) } + func testRangesInitializer() { + let ranges = [NSRange(0..<5), NSRange(5..<10)] + let set = IndexSet(ranges: ranges) + + XCTAssertEqual(set, IndexSet(integersIn: 0..<10)) + } + func testIntersets() { var set = IndexSet()