Skip to content

Commit d0f2b20

Browse files
authored
Merge pull request #1617 from spevans/pr_indexpath
2 parents a61b058 + 0f7ba9a commit d0f2b20

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

Foundation/NSIndexPath.swift

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
// This source file is part of the Swift.org open source project
22
//
3-
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
3+
// Copyright (c) 2014 - 2016, 2018 Apple Inc. and the Swift project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
6-
// See http://swift.org/LICENSE.txt for license information
7-
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
6+
// See https://swift.org/LICENSE.txt for license information
7+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

1010

1111
open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
1212

13-
internal var _indexes : [Int]
13+
private var _indexes : [Int]
14+
1415
override public init() {
1516
_indexes = []
1617
}
17-
public init(indexes: UnsafePointer<Int>!, length: Int) {
18-
_indexes = Array(UnsafeBufferPointer(start: indexes, count: length))
18+
19+
public init(indexes: UnsafePointer<Int>?, length: Int) {
20+
if length == 0 {
21+
_indexes = []
22+
} else {
23+
_indexes = Array(UnsafeBufferPointer(start: indexes!, count: length))
24+
}
1925
}
2026

2127
private init(indexes: [Int]) {
@@ -26,7 +32,10 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
2632
return copy(with: nil)
2733
}
2834

29-
open func copy(with zone: NSZone? = nil) -> Any { NSUnimplemented() }
35+
open func copy(with zone: NSZone? = nil) -> Any {
36+
return self
37+
}
38+
3039
public convenience init(index: Int) {
3140
self.init(indexes: [index])
3241
}
@@ -44,6 +53,7 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
4453
open func adding(_ index: Int) -> IndexPath {
4554
return IndexPath(indexes: _indexes + [index])
4655
}
56+
4757
open func removingLastIndex() -> IndexPath {
4858
if _indexes.count <= 1 {
4959
return IndexPath(indexes: [])
@@ -55,6 +65,7 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
5565
open func index(atPosition position: Int) -> Int {
5666
return _indexes[position]
5767
}
68+
5869
open var length: Int {
5970
return _indexes.count
6071
}
@@ -71,7 +82,12 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
7182
indexes.advanced(by: pos).pointee = idx
7283
}
7384
}
74-
85+
86+
@available(*, unavailable, renamed: "getIndex(_:range:)")
87+
open func getIndexes(_ indexes: UnsafeMutablePointer<Int>) {
88+
NSUnsupported()
89+
}
90+
7591
// comparison support
7692
// sorting an array of indexPaths using this comparison results in an array representing nodes in depth-first traversal order
7793
open func compare(_ otherObject: IndexPath) -> ComparisonResult {
@@ -96,10 +112,6 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
96112
}
97113
}
98114

99-
extension NSIndexPath {
100-
open func getIndexes(_ indexes: UnsafeMutablePointer<Int>) { NSUnimplemented() }
101-
}
102-
103115

104116
extension NSIndexPath : _StructTypeBridgeable {
105117
public typealias _StructType = IndexPath

TestFoundation/TestIndexPath.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// This source file is part of the Swift.org open source project
22
//
3-
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
3+
// Copyright (c) 2014 - 2016, 2018 Apple Inc. and the Swift project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
6-
// See http://swift.org/LICENSE.txt for license information
7-
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
6+
// See https://swift.org/LICENSE.txt for license information
7+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

1010
class TestIndexPath: XCTestCase {
@@ -59,12 +59,19 @@ class TestIndexPath: XCTestCase {
5959
("test_AnyHashableCreatedFromNSIndexPath", test_AnyHashableCreatedFromNSIndexPath),
6060
("test_unconditionallyBridgeFromObjectiveC", test_unconditionallyBridgeFromObjectiveC),
6161
("test_slice_1ary", test_slice_1ary),
62+
("test_copy", test_copy),
6263
]
6364
}
6465

6566
func testEmpty() {
6667
let ip = IndexPath()
6768
XCTAssertEqual(ip.count, 0)
69+
70+
// Darwin allows nil if length is 0
71+
let nsip = NSIndexPath(indexes: nil, length: 0)
72+
XCTAssertEqual(nsip.length, 0)
73+
let newIp = nsip.adding(1)
74+
XCTAssertEqual(newIp.count, 1)
6875
}
6976

7077
func testSingleIndex() {
@@ -764,4 +771,12 @@ class TestIndexPath: XCTestCase {
764771
XCTAssertEqual(0, slice.count)
765772
}
766773

774+
func test_copy() {
775+
var indexes = [1, 2, 3]
776+
let nip1 = NSIndexPath(indexes: &indexes, length: 3)
777+
let nip2 = nip1
778+
XCTAssertEqual(nip1.length, 3)
779+
XCTAssertEqual(nip2.length, 3)
780+
XCTAssertEqual(nip1, nip2)
781+
}
767782
}

0 commit comments

Comments
 (0)