Skip to content

Commit aea8c0b

Browse files
authored
Merge pull request #611 from gabhaska/nsindexset-branch
2 parents 16f83dd + afd48b8 commit aea8c0b

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Foundation/NSIndexSet.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
7878
return copy(with: nil)
7979
}
8080

81-
open func copy(with zone: NSZone? = nil) -> Any { NSUnimplemented() }
81+
open func copy(with zone: NSZone? = nil) -> Any {
82+
if type(of: self) === NSIndexSet.self {
83+
// return self for immutable type
84+
return self
85+
}
86+
return NSIndexSet(indexSet: self._bridgeToSwift())
87+
}
8288

8389
open override func mutableCopy() -> Any {
8490
return mutableCopy(with: nil)
@@ -505,6 +511,16 @@ open class NSMutableIndexSet : NSIndexSet {
505511
open func add(_ indexSet: IndexSet) {
506512
indexSet.rangeView.forEach { add(in: NSRange(location: $0.lowerBound, length: $0.upperBound - $0.lowerBound)) }
507513
}
514+
515+
open override func copy(with zone: NSZone? = nil) -> Any {
516+
if type(of: self) === NSMutableIndexSet.self {
517+
let indexSet = NSMutableIndexSet()
518+
indexSet._ranges = self._ranges
519+
indexSet._count = self._count
520+
return indexSet
521+
}
522+
return NSMutableIndexSet(indexSet: self._bridgeToSwift())
523+
}
508524

509525
open func remove(_ indexSet: IndexSet) {
510526
indexSet.rangeView.forEach { remove(in: NSRange(location: $0.lowerBound, length: $0.upperBound - $0.lowerBound)) }

TestFoundation/TestNSIndexSet.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TestNSIndexSet : XCTestCase {
2727
("test_removal", test_removal),
2828
("test_addition", test_addition),
2929
("test_setAlgebra", test_setAlgebra),
30+
("test_copy", test_copy),
3031
]
3132
}
3233

@@ -50,7 +51,29 @@ class TestNSIndexSet : XCTestCase {
5051
XCTAssertEqual(set3.lastIndex, 10)
5152

5253
}
53-
54+
55+
func test_copy() {
56+
let range: NSRange = NSRange(location: 3, length: 4)
57+
let array : [Int] = [1,2,3,4,5,6,7,8,9,10]
58+
let indexSet = NSMutableIndexSet()
59+
for index in array {
60+
indexSet.add(index)
61+
}
62+
63+
//Test copy operation of NSIndexSet case which is immutable
64+
let selfIndexSet: NSIndexSet = NSIndexSet(indexesIn: range)
65+
let selfIndexSetCopy = selfIndexSet.copy() as! NSIndexSet
66+
XCTAssertTrue(selfIndexSetCopy === selfIndexSet)
67+
XCTAssertTrue(selfIndexSetCopy.isEqual(to: selfIndexSet._bridgeToSwift()))
68+
69+
//Test copy operation of NSMutableIndexSet case
70+
let mutableIndexSet: NSIndexSet = indexSet
71+
indexSet.add(11)
72+
let mutableIndexSetCopy = mutableIndexSet.copy() as! NSIndexSet
73+
XCTAssertFalse(mutableIndexSetCopy === mutableIndexSet)
74+
XCTAssertTrue(mutableIndexSetCopy.isEqual(to: mutableIndexSet._bridgeToSwift()))
75+
}
76+
5477
func test_enumeration() {
5578
let set = IndexSet(integersIn: 4..<11)
5679
var result = Array<Int>()

0 commit comments

Comments
 (0)