Skip to content

Commit dc14510

Browse files
committed
Update NSIndexSet to match API exposed by Foundation
1 parent 03648e6 commit dc14510

File tree

3 files changed

+45
-46
lines changed

3 files changed

+45
-46
lines changed

Diff for: Foundation/NSArray.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
441441
guard !opts.contains(.concurrent) else {
442442
NSUnimplemented()
443443
}
444-
s._bridgeToObjectiveC().enumerate(opts) { (idx, stop) in
444+
s._bridgeToObjectiveC().enumerate(options: opts) { (idx, stop) in
445445
block(self.object(at: idx), idx, stop)
446446
}
447447
}

Diff for: Foundation/NSIndexSet.swift

+39-40
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ internal func __NSIndexSetIndexOfRangeContainingIndex(_ indexSet: NSIndexSet, _
5656
return UInt(bitPattern: NSNotFound)
5757
}
5858

59-
open class NSIndexSet: NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
59+
open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
6060
// all instance variables are private
6161

6262
internal var _ranges = [NSRange]()
@@ -86,7 +86,7 @@ open class NSIndexSet: NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
8686

8787
open func mutableCopy(with zone: NSZone? = nil) -> Any {
8888
let set = NSMutableIndexSet()
89-
enumerateRanges([]) {
89+
enumerateRanges(options: []) {
9090
set.add(in: $0.0)
9191
}
9292
return set
@@ -340,7 +340,7 @@ open class NSIndexSet: NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
340340
}
341341
open func contains(_ indexSet: IndexSet) -> Bool {
342342
var result = true
343-
enumerateRanges([]) { range, stop in
343+
enumerateRanges(options: []) { range, stop in
344344
if !self.contains(in: range) {
345345
result = false
346346
stop.pointee = true
@@ -415,20 +415,20 @@ open class NSIndexSet: NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
415415
return result
416416
}
417417

418-
public func enumerate(_ block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
419-
enumerate([], using: block)
418+
open func enumerate(_ block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
419+
enumerate(options: [], using: block)
420420
}
421-
public func enumerate(_ opts: NSEnumerationOptions = [], using block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
421+
open func enumerate(options opts: NSEnumerationOptions = [], using block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
422422
let _ = _enumerateWithOptions(opts, range: NSMakeRange(0, Int.max), paramType: Int.self, returnType: Void.self, block: block)
423423
}
424-
public func enumerate(in range: NSRange, options opts: NSEnumerationOptions = [], using block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
424+
open func enumerate(in range: NSRange, options opts: NSEnumerationOptions = [], using block: (Int, UnsafeMutablePointer<ObjCBool>) -> Void) {
425425
let _ = _enumerateWithOptions(opts, range: range, paramType: Int.self, returnType: Void.self, block: block)
426426
}
427427

428428
open func index(passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
429-
return index([], passingTest: predicate)
429+
return index(options: [], passingTest: predicate)
430430
}
431-
open func index(_ opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
431+
open func index(options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
432432
return _enumerateWithOptions(opts, range: NSMakeRange(0, Int.max), paramType: Int.self, returnType: Bool.self, block: predicate) ?? NSNotFound
433433
}
434434
open func index(in range: NSRange, options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> Int {
@@ -438,7 +438,7 @@ open class NSIndexSet: NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
438438
open func indexes(passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
439439
return indexes(in: NSMakeRange(0, Int.max), options: [], passingTest: predicate)
440440
}
441-
open func indexes(_ opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
441+
open func indexes(options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
442442
return indexes(in: NSMakeRange(0, Int.max), options: opts, passingTest: predicate)
443443
}
444444
open func indexes(in range: NSRange, options opts: NSEnumerationOptions = [], passingTest predicate: (Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> IndexSet {
@@ -456,47 +456,46 @@ open class NSIndexSet: NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
456456

457457
If the specified range for enumeration intersects a range of contiguous indexes in the receiver, then the block will be invoked with the intersection of those two ranges.
458458
*/
459-
public func enumerateRanges(_ block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
460-
enumerateRanges([], using: block)
459+
open func enumerateRanges(_ block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
460+
enumerateRanges(options: [], using: block)
461461
}
462-
public func enumerateRanges(_ opts: NSEnumerationOptions = [], using block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
462+
open func enumerateRanges(options opts: NSEnumerationOptions = [], using block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
463463
let _ = _enumerateWithOptions(opts, range: NSMakeRange(0, Int.max), paramType: NSRange.self, returnType: Void.self, block: block)
464464
}
465-
public func enumerateRanges(in range: NSRange, options opts: NSEnumerationOptions = [], using block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
465+
open func enumerateRanges(in range: NSRange, options opts: NSEnumerationOptions = [], using block: (NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
466466
let _ = _enumerateWithOptions(opts, range: range, paramType: NSRange.self, returnType: Void.self, block: block)
467467
}
468468
}
469469

470-
extension NSIndexSet: Sequence {
471-
472-
public struct Iterator : IteratorProtocol {
473-
internal let _set: NSIndexSet
474-
internal var _first: Bool = true
475-
internal var _current: Int?
476-
477-
internal init(_ set: NSIndexSet) {
478-
self._set = set
479-
self._current = nil
480-
}
481-
482-
public mutating func next() -> Int? {
483-
if _first {
484-
_current = _set.firstIndex
485-
_first = false
486-
} else if let c = _current {
487-
_current = _set.indexGreaterThanIndex(c)
488-
}
489-
if _current == NSNotFound {
490-
_current = nil
491-
}
492-
return _current
493-
}
470+
public struct NSIndexSetIterator : IteratorProtocol {
471+
public typealias Element = Int
472+
internal let _set: NSIndexSet
473+
internal var _first: Bool = true
474+
internal var _current: Element?
475+
476+
internal init(_ set: NSIndexSet) {
477+
self._set = set
478+
self._current = nil
494479
}
495480

496-
public func makeIterator() -> Iterator {
497-
return Iterator(self)
481+
public mutating func next() -> Element? {
482+
if _first {
483+
_current = _set.firstIndex
484+
_first = false
485+
} else if let c = _current {
486+
_current = _set.indexGreaterThanIndex(c)
487+
}
488+
if _current == NSNotFound {
489+
_current = nil
490+
}
491+
return _current
498492
}
493+
}
499494

495+
extension NSIndexSet : Sequence {
496+
public func makeIterator() -> NSIndexSetIterator {
497+
return NSIndexSetIterator(self)
498+
}
500499
}
501500

502501
open class NSMutableIndexSet : NSIndexSet {

Diff for: TestFoundation/TestNSIndexSet.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class TestNSIndexSet : XCTestCase {
7373
disjointSet.add(11)
7474
disjointSet.add(in: NSMakeRange(13, 2))
7575
result = Array<Int>()
76-
disjointSet.enumerate([]) { (idx, _) in
76+
disjointSet.enumerate(options: []) { (idx, _) in
7777
result.append(idx)
7878
}
7979
XCTAssertEqual(result, [2, 5, 7, 8, 9, 11, 13, 14])
@@ -118,7 +118,7 @@ class TestNSIndexSet : XCTestCase {
118118
var i = 0
119119

120120
if testInputA1.count == testSetA.count {
121-
testSetA.enumerate([]) { (idx, _) in
121+
testSetA.enumerate(options: []) { (idx, _) in
122122
XCTAssertEqual(idx, testInputA1[i])
123123
i += 1
124124
}
@@ -131,7 +131,7 @@ class TestNSIndexSet : XCTestCase {
131131
let testInputA2 = [NSMakeRange(0, 1),NSMakeRange(5, 4),NSMakeRange(42, 1)]
132132
i = 0
133133

134-
testSetA.enumerateRanges([]) { (range, _) in
134+
testSetA.enumerateRanges(options: []) { (range, _) in
135135
let testRange = testInputA2[i]
136136
XCTAssertEqual(range.location, testRange.location)
137137
XCTAssertEqual(range.length, testRange.length)
@@ -147,7 +147,7 @@ class TestNSIndexSet : XCTestCase {
147147
i = 0
148148

149149
if testInputB1.count == testSetB.count {
150-
testSetB.enumerate([]) { (idx, _) in
150+
testSetB.enumerate(options: []) { (idx, _) in
151151
XCTAssertEqual(idx, testInputB1[i])
152152
i += 1
153153
}
@@ -160,7 +160,7 @@ class TestNSIndexSet : XCTestCase {
160160
let testInputB2 = [NSMakeRange(0, 5),NSMakeRange(18, 1),NSMakeRange(42, 3)]
161161
i = 0
162162

163-
testSetB.enumerateRanges([]) { (range, _) in
163+
testSetB.enumerateRanges(options: []) { (range, _) in
164164
let testRange = testInputB2[i]
165165
XCTAssertEqual(range.location, testRange.location)
166166
XCTAssertEqual(range.length, testRange.length)

0 commit comments

Comments
 (0)