Skip to content

Commit 0dfe36a

Browse files
authored
Merge pull request #627 from saiHemak/nsdictWriteTo-branch
2 parents 70db425 + d47db67 commit 0dfe36a

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

Foundation/NSDictionary.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,20 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
433433
return objects
434434
}
435435

436-
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool { NSUnimplemented() }
437-
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.
436+
open func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool {
437+
return write(to: URL(fileURLWithPath: path), atomically: useAuxiliaryFile)
438+
}
439+
440+
// the atomically flag is ignored if url of a type that cannot be written atomically.
441+
open func write(to url: URL, atomically: Bool) -> Bool {
442+
do {
443+
let pListData = try PropertyListSerialization.data(fromPropertyList: self, format: PropertyListSerialization.PropertyListFormat.xml, options: 0)
444+
try pListData.write(to: url, options: atomically ? .atomic : [])
445+
return true
446+
} catch {
447+
return false
448+
}
449+
}
438450

439451
open func enumerateKeysAndObjects(_ block: (Any, Any, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
440452
enumerateKeysAndObjects(options: [], using: block)

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

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

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

0 commit comments

Comments
 (0)