Skip to content

NSIndexPath: Make compatible with Darwin. #1617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions Foundation/NSIndexPath.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2016, 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//


open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {

internal var _indexes : [Int]
private var _indexes : [Int]

override public init() {
_indexes = []
}
public init(indexes: UnsafePointer<Int>!, length: Int) {
_indexes = Array(UnsafeBufferPointer(start: indexes, count: length))

public init(indexes: UnsafePointer<Int>?, length: Int) {
if length == 0 {
_indexes = []
} else {
_indexes = Array(UnsafeBufferPointer(start: indexes!, count: length))
}
}

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

open func copy(with zone: NSZone? = nil) -> Any { NSUnimplemented() }
open func copy(with zone: NSZone? = nil) -> Any {
return self
}

public convenience init(index: Int) {
self.init(indexes: [index])
}
Expand All @@ -44,6 +53,7 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
open func adding(_ index: Int) -> IndexPath {
return IndexPath(indexes: _indexes + [index])
}

open func removingLastIndex() -> IndexPath {
if _indexes.count <= 1 {
return IndexPath(indexes: [])
Expand All @@ -55,6 +65,7 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
open func index(atPosition position: Int) -> Int {
return _indexes[position]
}

open var length: Int {
return _indexes.count
}
Expand All @@ -71,7 +82,12 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
indexes.advanced(by: pos).pointee = idx
}
}


@available(*, unavailable, renamed: "getIndex(_:range:)")
open func getIndexes(_ indexes: UnsafeMutablePointer<Int>) {
NSUnsupported()
}

// comparison support
// sorting an array of indexPaths using this comparison results in an array representing nodes in depth-first traversal order
open func compare(_ otherObject: IndexPath) -> ComparisonResult {
Expand All @@ -96,10 +112,6 @@ open class NSIndexPath : NSObject, NSCopying, NSSecureCoding {
}
}

extension NSIndexPath {
open func getIndexes(_ indexes: UnsafeMutablePointer<Int>) { NSUnimplemented() }
}


extension NSIndexPath : _StructTypeBridgeable {
public typealias _StructType = IndexPath
Expand Down
21 changes: 18 additions & 3 deletions TestFoundation/TestIndexPath.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2016, 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

class TestIndexPath: XCTestCase {
Expand Down Expand Up @@ -59,12 +59,19 @@ class TestIndexPath: XCTestCase {
("test_AnyHashableCreatedFromNSIndexPath", test_AnyHashableCreatedFromNSIndexPath),
("test_unconditionallyBridgeFromObjectiveC", test_unconditionallyBridgeFromObjectiveC),
("test_slice_1ary", test_slice_1ary),
("test_copy", test_copy),
]
}

func testEmpty() {
let ip = IndexPath()
XCTAssertEqual(ip.count, 0)

// Darwin allows nil if length is 0
let nsip = NSIndexPath(indexes: nil, length: 0)
XCTAssertEqual(nsip.length, 0)
let newIp = nsip.adding(1)
XCTAssertEqual(newIp.count, 1)
}

func testSingleIndex() {
Expand Down Expand Up @@ -764,4 +771,12 @@ class TestIndexPath: XCTestCase {
XCTAssertEqual(0, slice.count)
}

func test_copy() {
var indexes = [1, 2, 3]
let nip1 = NSIndexPath(indexes: &indexes, length: 3)
let nip2 = nip1
XCTAssertEqual(nip1.length, 3)
XCTAssertEqual(nip2.length, 3)
XCTAssertEqual(nip1, nip2)
}
}