Skip to content

Commit 4e92123

Browse files
authored
Merge pull request #1315 from bubski/insert-at-indexes
2 parents abbbbc3 + c96bc21 commit 4e92123

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

Foundation/NSArray.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,17 @@ open class NSMutableArray : NSArray {
717717
}
718718

719719
open func insert(_ objects: [Any], at indexes: IndexSet) {
720-
NSUnimplemented()
720+
precondition(objects.count == indexes.count)
721+
722+
if type(of: self) === NSMutableArray.self {
723+
_storage.reserveCapacity(count + indexes.count)
724+
}
725+
726+
var objectIdx = 0
727+
for insertionIndex in indexes {
728+
self.insert(objects[objectIdx], at: insertionIndex)
729+
objectIdx += 1
730+
}
721731
}
722732

723733
open func removeLastObject() {
@@ -878,27 +888,13 @@ open class NSMutableArray : NSArray {
878888
}
879889
}
880890

881-
open func insertObjects(_ objects: [Any], atIndexes indexes: IndexSet) {
882-
precondition(objects.count == indexes.count)
883-
884-
if type(of: self) === NSMutableArray.self {
885-
_storage.reserveCapacity(count + indexes.count)
886-
}
887-
888-
var objectIdx = 0
889-
for insertionIndex in indexes {
890-
self.insert(objects[objectIdx], at: insertionIndex)
891-
objectIdx += 1
892-
}
893-
}
894-
895891
open func removeObjects(at indexes: IndexSet) {
896892
for range in indexes.rangeView.reversed() {
897893
self.removeObjects(in: NSMakeRange(range.lowerBound, range.upperBound - range.lowerBound))
898894
}
899895
}
900896

901-
open func replaceObjects(at indexes: IndexSet, withObjects objects: [Any]) {
897+
open func replaceObjects(at indexes: IndexSet, with objects: [Any]) {
902898
var objectIndex = 0
903899
for countedRange in indexes.rangeView {
904900
let range = NSMakeRange(countedRange.lowerBound, countedRange.upperBound - countedRange.lowerBound)

TestFoundation/TestNSArray.swift

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ class TestNSArray : XCTestCase {
4646
("test_mutableCopying", test_mutableCopying),
4747
("test_writeToFile", test_writeToFile),
4848
("test_initWithContentsOfFile", test_initWithContentsOfFile),
49-
("test_readWriteURL", test_readWriteURL)
49+
("test_readWriteURL", test_readWriteURL),
50+
("test_insertObjectAtIndex", test_insertObjectAtIndex),
51+
("test_insertObjectsAtIndexes", test_insertObjectsAtIndexes),
52+
("test_replaceObjectsAtIndexesWithObjects", test_replaceObjectsAtIndexesWithObjects),
5053
]
5154
}
5255

@@ -565,6 +568,52 @@ class TestNSArray : XCTestCase {
565568
}
566569
}
567570

571+
func test_insertObjectAtIndex() {
572+
let a1 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
573+
a1.insert("a", at: 0)
574+
XCTAssertEqual(a1, ["a", "one", "two", "three", "four"])
575+
576+
let a2 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
577+
a2.insert("a", at: 4)
578+
XCTAssertEqual(a2, ["one", "two", "three", "four", "a"])
579+
580+
let a3 = NSMutableArray()
581+
a3.insert("a", at: 0)
582+
XCTAssertEqual(a3, ["a"])
583+
}
584+
585+
func test_insertObjectsAtIndexes() {
586+
let a1 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
587+
a1.insert(["a", "b"], at: [0, 1])
588+
XCTAssertEqual(a1, ["a", "b", "one", "two", "three", "four"])
589+
590+
let a2 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
591+
a2.insert(["a", "b"], at: [1, 3])
592+
XCTAssertEqual(a2, ["one", "a", "two", "b", "three", "four"])
593+
594+
let a3 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
595+
a3.insert(["a", "b"], at: [5, 4])
596+
XCTAssertEqual(a3, ["one", "two", "three", "four", "a", "b"])
597+
598+
let a4 = NSMutableArray()
599+
a4.insert(["a", "b"], at: [0, 1])
600+
XCTAssertEqual(a4, ["a", "b"])
601+
}
602+
603+
func test_replaceObjectsAtIndexesWithObjects() {
604+
let a1 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
605+
a1.replaceObjects(at: [0], with: ["a"])
606+
XCTAssertEqual(a1, ["a", "two", "three", "four"])
607+
608+
let a2 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
609+
a2.replaceObjects(at: [1, 2], with: ["a", "b"])
610+
XCTAssertEqual(a2, ["one", "a", "b", "four"])
611+
612+
let a3 = NSMutableArray(arrayLiteral: "one", "two", "three", "four")
613+
a3.replaceObjects(at: [3, 2, 1, 0], with: ["a", "b", "c", "d"])
614+
XCTAssertEqual(a3, ["a", "b", "c", "d"])
615+
}
616+
568617
private func createTestFile(_ path: String, _contents: Data) -> String? {
569618
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
570619
do {

0 commit comments

Comments
 (0)