Skip to content

Implementation for NSDictionary.write(toFile:) #627

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 14, 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 @@ -433,8 +433,20 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
return objects
}

open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool { NSUnimplemented() }
open func write(to url: URL, atomically: Bool) -> Bool { NSUnimplemented() } // the atomically flag is ignored if url of a type that cannot be written atomically.
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
return write(to: URL(fileURLWithPath: path), atomically: useAuxiliaryFile)
}

// the atomically flag is ignored if url of a type that cannot be written atomically.
open func write(to url: URL, atomically: Bool) -> Bool {
do {
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: PropertyListSerialization.PropertyListFormat.xml, options: 0)
try pListData.write(to: url, options: atomically ? .atomic : [])
return true
} catch {
return false
}
}

open func enumerateKeysAndObjects(_ block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
enumerateKeysAndObjects(options: [], using: block)
Expand Down
49 changes: 49 additions & 0 deletions TestFoundation/TestNSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TestNSDictionary : XCTestCase {
("test_equality", test_equality),
("test_copying", test_copying),
("test_mutableCopying", test_mutableCopying),
("test_writeToFile", test_writeToFile),
]
}

Expand Down Expand Up @@ -161,4 +162,52 @@ class TestNSDictionary : XCTestCase {
XCTAssertTrue(dictMutableCopy2 == dictMutableCopy1)
}

func test_writeToFile() {
let testFilePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
if let _ = testFilePath {
let d1: NSDictionary = [ "foo": "bar", "baz": "qux"]
let isWritten = d1.write(toFile: testFilePath!, atomically: true)
if(isWritten){
do{
let plistDoc = try XMLDocument(contentsOf: URL(fileURLWithPath: testFilePath!, isDirectory: false), options: [])
try plistDoc.validate()
XCTAssert(plistDoc.rootElement()?.name == "plist")
let plist = try PropertyListSerialization.propertyList(from: plistDoc.xmlData, options: [], format: nil) as! [String: Any]
XCTAssert((plist["foo"] as? String) == d1["foo"] as? String)
XCTAssert((plist["baz"] as? String) == d1["baz"] as? String)
} catch {
XCTFail("XMLDocument failes to read / validate contenets")
}
} 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 + "/"
do {
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
if FileManager.default.createFile(atPath: tempDir + "/" + path, contents: _contents,
attributes: nil) {
return tempDir + path
} else {
return nil
}
} catch _ {
return nil
}
}

private func removeTestFile(_ location: String) {
do {
try FileManager.default.removeItem(atPath: location)
} catch _ {

}
}

}