Skip to content

Commit 68ce623

Browse files
saiHemakparkera
authored andcommitted
Implementing NSDictionary.descriptionInStringsFileFormat (#623)
Fix for displaying nested dictionaries properly
1 parent 19215d0 commit 68ce623

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

Foundation/NSDictionary.swift

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,55 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
231231
open override var description: String {
232232
return description(withLocale: nil)
233233
}
234+
235+
private func getDescription(of object: Any) -> String? {
236+
switch object {
237+
case is NSArray.Type:
238+
return (object as! NSArray).description(withLocale: nil, indent: 1)
239+
case is NSDecimalNumber.Type:
240+
return (object as! NSDecimalNumber).description(withLocale: nil)
241+
case is NSDate.Type:
242+
return (object as! NSDate).description(with: nil)
243+
case is NSOrderedSet.Type:
244+
return (object as! NSOrderedSet).description(withLocale: nil)
245+
case is NSSet.Type:
246+
return (object as! NSSet).description(withLocale: nil)
247+
case is NSDictionary.Type:
248+
return (object as! NSDictionary).description(withLocale: nil)
249+
default:
250+
if let hashableObject = object as? Dictionary<AnyHashable, Any> {
251+
return hashableObject._nsObject.description(withLocale: nil, indent: 1)
252+
} else {
253+
return nil
254+
}
255+
}
256+
}
234257

235-
open var descriptionInStringsFileFormat: String { NSUnimplemented() }
258+
open var descriptionInStringsFileFormat: String {
259+
var lines = [String]()
260+
for key in self.allKeys {
261+
let line = NSMutableString(capacity: 0)
262+
line.append("\"")
263+
if let descriptionByType = getDescription(of: key) {
264+
line.append(descriptionByType)
265+
} else {
266+
line.append("\(key)")
267+
}
268+
line.append("\"")
269+
line.append(" = ")
270+
line.append("\"")
271+
let value = self.object(forKey: key)!
272+
if let descriptionByTypeValue = getDescription(of: value) {
273+
line.append(descriptionByTypeValue)
274+
} else {
275+
line.append("\(value)")
276+
}
277+
line.append("\"")
278+
line.append(";")
279+
lines.append(line._bridgeToSwift())
280+
}
281+
return lines.joined(separator: "\n")
282+
}
236283

237284
/// Returns a string object that represents the contents of the dictionary,
238285
/// formatted as a property list.
@@ -298,11 +345,14 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
298345
} else if object is NSSet {
299346
line += (object as! NSSet).description(withLocale: locale)
300347
} else {
301-
line += "\(object)"
348+
if let hashableObject = object as? Dictionary<AnyHashable, Any> {
349+
line += hashableObject._nsObject.description(withLocale: nil, indent: level+1)
350+
} else {
351+
line += "\(object)"
352+
}
302353
}
303354

304355
line += ";"
305-
306356
lines.append(line)
307357
}
308358

TestFoundation/TestNSDictionary.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ class TestNSDictionary : XCTestCase {
4444
func test_description() {
4545
let d1: NSDictionary = [ "foo": "bar", "baz": "qux"]
4646
XCTAssertEqual(d1.description, "{\n baz = qux;\n foo = bar;\n}")
47-
let _: NSDictionary = ["1" : ["1" : ["1" : "1"]]]
48-
// Disabled since it emits AnyHashable in the description for now...
49-
// XCTAssertEqual(d2.description, "{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}")
47+
let d2: NSDictionary = ["1" : ["1" : ["1" : "1"]]]
48+
XCTAssertEqual(d2.description, "{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}")
5049
}
5150

5251
func test_HeterogeneousConstruction() {

0 commit comments

Comments
 (0)