Skip to content

Implementation of NSMutableDictionary.init(contentsOfFile:) #639

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
Sep 22, 2016
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
16 changes: 14 additions & 2 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,20 @@ open class NSMutableDictionary : NSDictionary {
super.init(objects: objects, forKeys: keys, count: cnt)
}

public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
public convenience init?(contentsOfURL url: URL) { NSUnimplemented() }
public convenience init?(contentsOfFile path: String) {
self.init(contentsOfURL: URL(fileURLWithPath: path))
}

public convenience init?(contentsOfURL url: URL) {
do {
guard let plistDoc = try? Data(contentsOf: url) else { return nil }
let plistDict = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Dictionary<AnyHashable,Any>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NSMutableDictionary technically will use NSPropertyListMutableContainers however I am not fully certain how much value that adds since all struct Dictionaries are mutable via var declarations

guard let plistDictionary = plistDict else { return nil }
self.init(dictionary: plistDictionary)
} catch {
return nil
}
}
}

extension NSMutableDictionary {
Expand Down
18 changes: 18 additions & 0 deletions TestFoundation/TestNSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TestNSDictionary : XCTestCase {
("test_copying", test_copying),
("test_mutableCopying", test_mutableCopying),
("test_writeToFile", test_writeToFile),
("test_initWithContentsOfFile", test_initWithContentsOfFile),
]
}

Expand Down Expand Up @@ -186,6 +187,23 @@ class TestNSDictionary : XCTestCase {
XCTFail("Temporary file creation failed")
}
}

func test_initWithContentsOfFile() {
let testFilePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
if let _ = testFilePath {
let d1: NSDictionary = ["Hello":["world":"again"]]
let isWritten = d1.write(toFile: testFilePath!, atomically: true)
if(isWritten) {
let dict = NSMutableDictionary.init(contentsOfFile: testFilePath!)
XCTAssert(dict == d1)
} else {
XCTFail("Write to file failed")
}
removeTestFile(testFilePath!)
} else {
XCTFail("Temporary file creation failed")
}
}

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