Skip to content

Commit 5dd4e25

Browse files
author
David Dunn
committed
Moving init?(ContentsOfFile:) to NSDictionary
1 parent 287ecb7 commit 5dd4e25

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

Foundation/NSDictionary.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
3939

4040
return NSGeneratorEnumerator(_storage.keys.map { _SwiftValue.fetch(nonOptional: $0) }.makeIterator())
4141
}
42+
@available(*, deprecated)
43+
public convenience init?(contentsOfFile path: String) {
44+
self.init(contentsOfURL: URL(fileURLWithPath: path))
45+
}
46+
47+
@available(*, deprecated)
48+
public convenience init?(contentsOfURL url: URL) {
49+
do {
50+
guard let plistDoc = try? Data(contentsOf: url) else { return nil }
51+
let plistDict = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Dictionary<AnyHashable,Any>
52+
guard let plistDictionary = plistDict else { return nil }
53+
self.init(dictionary: plistDictionary)
54+
} catch {
55+
return nil
56+
}
57+
}
4258

4359
public override convenience init() {
4460
self.init(objects: [], forKeys: [], count: 0)
@@ -583,20 +599,6 @@ open class NSMutableDictionary : NSDictionary {
583599
super.init(objects: objects, forKeys: keys, count: cnt)
584600
}
585601

586-
public convenience init?(contentsOfFile path: String) {
587-
self.init(contentsOfURL: URL(fileURLWithPath: path))
588-
}
589-
590-
public convenience init?(contentsOfURL url: URL) {
591-
do {
592-
guard let plistDoc = try? Data(contentsOf: url) else { return nil }
593-
let plistDict = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Dictionary<AnyHashable,Any>
594-
guard let plistDictionary = plistDict else { return nil }
595-
self.init(dictionary: plistDictionary)
596-
} catch {
597-
return nil
598-
}
599-
}
600602
}
601603

602604
extension NSMutableDictionary {

TestFoundation/TestNSDictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class TestNSDictionary : XCTestCase {
211211
let d1: NSDictionary = ["Hello":["world":"again"]]
212212
let isWritten = d1.write(toFile: testFilePath!, atomically: true)
213213
if(isWritten) {
214-
let dict = NSMutableDictionary.init(contentsOfFile: testFilePath!)
214+
let dict = NSDictionary(contentsOfFile: testFilePath!)
215215
XCTAssert(dict == d1)
216216
} else {
217217
XCTFail("Write to file failed")

0 commit comments

Comments
 (0)