Skip to content

Commit a81cb15

Browse files
committed
Implementation for NSDictionary.write(toFile:)
1 parent 2105e9c commit a81cb15

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Foundation/NSDictionary.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,18 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
400400
return objects
401401
}
402402

403-
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool { NSUnimplemented() }
403+
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
404+
var returnVal = false
405+
do {
406+
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: PropertyListSerialization.PropertyListFormat.xml, options: 0)
407+
let xmlDocument = try XMLDocument.init(data: pListData, options: XMLNode.Options.documentValidate)
408+
let xmlData = xmlDocument.xmlData(withOptions: XMLNode.Options.nodePrettyPrint)
409+
try xmlData.write(to: URL(fileURLWithPath: path), options: useAuxiliaryFile ? .atomic : [])
410+
returnVal = true
411+
} catch {
412+
}
413+
return returnVal
414+
}
404415
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.
405416

406417
open func enumerateKeysAndObjects(_ block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {

TestFoundation/TestNSDictionary.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TestNSDictionary : XCTestCase {
3030
("test_equality", test_equality),
3131
("test_copying", test_copying),
3232
("test_mutableCopying", test_mutableCopying),
33+
("test_writeToFile", test_writeToFile),
3334
]
3435
}
3536

@@ -162,4 +163,52 @@ class TestNSDictionary : XCTestCase {
162163
XCTAssertTrue(dictMutableCopy2 == dictMutableCopy1)
163164
}
164165

166+
func test_writeToFile() {
167+
let testFilePath = createTestFile("TestFileOut.txt", _contents: Data(capacity: 256))
168+
if let _ = testFilePath {
169+
let d1: NSDictionary = [ "foo": "bar", "baz": "qux"]
170+
let isWritten = d1.write(toFile: testFilePath!, atomically: true)
171+
if(isWritten){
172+
do{
173+
let plistDoc = try XMLDocument(contentsOf: URL(fileURLWithPath: testFilePath!, isDirectory: false), options: [])
174+
try plistDoc.validate()
175+
XCTAssert(plistDoc.rootElement()?.name == "plist")
176+
let plist = try PropertyListSerialization.propertyList(from: plistDoc.xmlData, options: [], format: nil) as! [String: Any]
177+
XCTAssert((plist["foo"] as? String) == d1["foo"] as? String)
178+
XCTAssert((plist["baz"] as? String) == d1["baz"] as? String)
179+
} catch {
180+
XCTFail("XMLDocument failes to read / validate contenets")
181+
}
182+
} else {
183+
XCTFail("Write to file failed")
184+
}
185+
removeTestFile(testFilePath!)
186+
} else {
187+
XCTFail("Temporary file creation failed")
188+
}
189+
}
190+
191+
private func createTestFile(_ path: String, _contents: Data) -> String? {
192+
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().uuidString + "/"
193+
do {
194+
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
195+
if FileManager.default.createFile(atPath: tempDir + "/" + path, contents: _contents,
196+
attributes: nil) {
197+
return tempDir + path
198+
} else {
199+
return nil
200+
}
201+
} catch _ {
202+
return nil
203+
}
204+
}
205+
206+
private func removeTestFile(_ location: String) {
207+
do {
208+
try FileManager.default.removeItem(atPath: location)
209+
} catch _ {
210+
211+
}
212+
}
213+
165214
}

0 commit comments

Comments
 (0)