Skip to content

Update API names for NSMutableArray #1315

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 1 commit into from
Nov 16, 2017
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
28 changes: 12 additions & 16 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,17 @@ open class NSMutableArray : NSArray {
}

open func insert(_ objects: [Any], at indexes: IndexSet) {
NSUnimplemented()
precondition(objects.count == indexes.count)

if type(of: self) === NSMutableArray.self {
_storage.reserveCapacity(count + indexes.count)
}

var objectIdx = 0
for insertionIndex in indexes {
self.insert(objects[objectIdx], at: insertionIndex)
objectIdx += 1
}
}

open func removeLastObject() {
Expand Down Expand Up @@ -878,27 +888,13 @@ open class NSMutableArray : NSArray {
}
}

open func insertObjects(_ objects: [Any], atIndexes indexes: IndexSet) {
precondition(objects.count == indexes.count)

if type(of: self) === NSMutableArray.self {
_storage.reserveCapacity(count + indexes.count)
}

var objectIdx = 0
for insertionIndex in indexes {
self.insert(objects[objectIdx], at: insertionIndex)
objectIdx += 1
}
}

open func removeObjects(at indexes: IndexSet) {
for range in indexes.rangeView.reversed() {
self.removeObjects(in: NSMakeRange(range.lowerBound, range.upperBound - range.lowerBound))
}
}

open func replaceObjects(at indexes: IndexSet, withObjects objects: [Any]) {
open func replaceObjects(at indexes: IndexSet, with objects: [Any]) {
var objectIndex = 0
for countedRange in indexes.rangeView {
let range = NSMakeRange(countedRange.lowerBound, countedRange.upperBound - countedRange.lowerBound)
Expand Down
51 changes: 50 additions & 1 deletion TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class TestNSArray : XCTestCase {
("test_mutableCopying", test_mutableCopying),
("test_writeToFile", test_writeToFile),
("test_initWithContentsOfFile", test_initWithContentsOfFile),
("test_readWriteURL", test_readWriteURL)
("test_readWriteURL", test_readWriteURL),
("test_insertObjectAtIndex", test_insertObjectAtIndex),
("test_insertObjectsAtIndexes", test_insertObjectsAtIndexes),
("test_replaceObjectsAtIndexesWithObjects", test_replaceObjectsAtIndexesWithObjects),
]
}

Expand Down Expand Up @@ -565,6 +568,52 @@ class TestNSArray : XCTestCase {
}
}

func test_insertObjectAtIndex() {
let a1 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a1.insert("a", at: 0)
XCTAssertEqual(a1, ["a", "one", "two", "three", "four"])

let a2 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a2.insert("a", at: 4)
XCTAssertEqual(a2, ["one", "two", "three", "four", "a"])

let a3 = NSMutableArray()
a3.insert("a", at: 0)
XCTAssertEqual(a3, ["a"])
}

func test_insertObjectsAtIndexes() {
let a1 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a1.insert(["a", "b"], at: [0, 1])
XCTAssertEqual(a1, ["a", "b", "one", "two", "three", "four"])

let a2 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a2.insert(["a", "b"], at: [1, 3])
XCTAssertEqual(a2, ["one", "a", "two", "b", "three", "four"])

let a3 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a3.insert(["a", "b"], at: [5, 4])
XCTAssertEqual(a3, ["one", "two", "three", "four", "a", "b"])

let a4 = NSMutableArray()
a4.insert(["a", "b"], at: [0, 1])
XCTAssertEqual(a4, ["a", "b"])
}

func test_replaceObjectsAtIndexesWithObjects() {
let a1 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a1.replaceObjects(at: [0], with: ["a"])
XCTAssertEqual(a1, ["a", "two", "three", "four"])

let a2 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a2.replaceObjects(at: [1, 2], with: ["a", "b"])
XCTAssertEqual(a2, ["one", "a", "b", "four"])

let a3 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
a3.replaceObjects(at: [3, 2, 1, 0], with: ["a", "b", "c", "d"])
XCTAssertEqual(a3, ["a", "b", "c", "d"])
}

private func createTestFile(_ path: String, _contents: Data) -> String? {
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
do {
Expand Down