Skip to content

Commit 516dd71

Browse files
authored
Merge pull request #1411 from spevans/pr_nsrray_contentsof
2 parents e0e0873 + 2e3377c commit 516dd71

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Foundation/NSArray.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,6 @@ open class NSMutableArray : NSArray {
934934
open func sort(options opts: NSSortOptions = [], usingComparator cmptr: Comparator) {
935935
self.setArray(self.sortedArray(options: opts, usingComparator: cmptr))
936936
}
937-
938-
public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
939-
public convenience init?(contentsOfURL url: URL) { NSUnimplemented() }
940937
}
941938

942939
extension NSArray : Sequence {

TestFoundation/TestNSArray.swift

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class TestNSArray : XCTestCase {
4747
("test_mutableCopying", test_mutableCopying),
4848
("test_writeToFile", test_writeToFile),
4949
("test_initWithContentsOfFile", test_initWithContentsOfFile),
50+
("test_initMutableWithContentsOfFile", test_initMutableWithContentsOfFile),
51+
("test_initMutableWithContentsOfURL", test_initMutableWithContentsOfURL),
5052
("test_readWriteURL", test_readWriteURL),
5153
("test_insertObjectAtIndex", test_insertObjectAtIndex),
5254
("test_insertObjectsAtIndexes", test_insertObjectsAtIndexes),
@@ -647,7 +649,46 @@ class TestNSArray : XCTestCase {
647649
XCTFail("Temporary file creation failed")
648650
}
649651
}
650-
652+
653+
func test_initMutableWithContentsOfFile() {
654+
if let testFilePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 234)) {
655+
let a1: NSArray = ["foo", "bar"]
656+
let isWritten = a1.write(toFile: testFilePath, atomically: true)
657+
if isWritten {
658+
let array = NSMutableArray.init(contentsOfFile: testFilePath)
659+
XCTAssert(array == a1)
660+
XCTAssertEqual(array?.count, 2)
661+
array?.removeAllObjects()
662+
XCTAssertEqual(array?.count, 0)
663+
} else {
664+
XCTFail("Write to file failed")
665+
}
666+
removeTestFile(testFilePath)
667+
} else {
668+
XCTFail("Temporary file creation failed")
669+
}
670+
}
671+
672+
func test_initMutableWithContentsOfURL() {
673+
if let testFilePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 234)) {
674+
let a1: NSArray = ["foo", "bar"]
675+
let isWritten = a1.write(toFile: testFilePath, atomically: true)
676+
if isWritten {
677+
let url = URL(fileURLWithPath: testFilePath, isDirectory: false)
678+
let array = NSMutableArray.init(contentsOf: url)
679+
XCTAssert(array == a1)
680+
XCTAssertEqual(array?.count, 2)
681+
array?.removeAllObjects()
682+
XCTAssertEqual(array?.count, 0)
683+
} else {
684+
XCTFail("Write to file failed")
685+
}
686+
removeTestFile(testFilePath)
687+
} else {
688+
XCTFail("Temporary file creation failed")
689+
}
690+
}
691+
651692
func test_writeToFile() {
652693
let testFilePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 234))
653694
if let _ = testFilePath {

0 commit comments

Comments
 (0)