From c96bc21ea2bd8d342f5c59e8d63f4b66774f6a0b Mon Sep 17 00:00:00 2001 From: Bartek Chlebek Date: Sat, 11 Nov 2017 22:27:50 +0100 Subject: [PATCH] Update API names for NSMutableArray --- Foundation/NSArray.swift | 28 ++++++++---------- TestFoundation/TestNSArray.swift | 51 +++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/Foundation/NSArray.swift b/Foundation/NSArray.swift index d004eccb00..33b4b015b7 100644 --- a/Foundation/NSArray.swift +++ b/Foundation/NSArray.swift @@ -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() { @@ -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) diff --git a/TestFoundation/TestNSArray.swift b/TestFoundation/TestNSArray.swift index 4ea68180af..e1dcf01b0d 100644 --- a/TestFoundation/TestNSArray.swift +++ b/TestFoundation/TestNSArray.swift @@ -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), ] } @@ -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 {