Skip to content

Commit 561c75b

Browse files
authored
Merge pull request #1238 from ddunn2/nsdictionary
Moving init?(ContentsOfFile:) to NSDictionary
2 parents 42d5ddf + dd3100b commit 561c75b

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

Foundation/NSDictionary.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
4444
return NSGeneratorEnumerator(_storage.keys.map { _SwiftValue.fetch(nonOptional: $0) }.makeIterator())
4545
}
4646

47+
@available(*, deprecated)
48+
public convenience init?(contentsOfFile path: String) {
49+
self.init(contentsOf: URL(fileURLWithPath: path))
50+
}
51+
52+
@available(*, deprecated)
53+
public convenience init?(contentsOf url: URL) {
54+
do {
55+
guard let plistDoc = try? Data(contentsOf: url) else { return nil }
56+
let plistDict = try PropertyListSerialization.propertyList(from: plistDoc, options: [], format: nil) as? Dictionary<AnyHashable,Any>
57+
guard let plistDictionary = plistDict else { return nil }
58+
self.init(dictionary: plistDictionary)
59+
} catch {
60+
return nil
61+
}
62+
}
63+
4764
public override convenience init() {
4865
self.init(objects: [], forKeys: [], count: 0)
4966
}
@@ -587,20 +604,6 @@ open class NSMutableDictionary : NSDictionary {
587604
super.init(objects: objects, forKeys: keys, count: cnt)
588605
}
589606

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

606609
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)