Skip to content

Implementing NSDictionary.descriptionInStringsFileFormat #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,55 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
open override var description: String {
return description(withLocale: nil)
}

private func getDescription(of object: Any) -> String? {
switch object {
case is NSArray.Type:
return (object as! NSArray).description(withLocale: nil, indent: 1)
case is NSDecimalNumber.Type:
return (object as! NSDecimalNumber).description(withLocale: nil)
case is NSDate.Type:
return (object as! NSDate).description(with: nil)
case is NSOrderedSet.Type:
return (object as! NSOrderedSet).description(withLocale: nil)
case is NSSet.Type:
return (object as! NSSet).description(withLocale: nil)
case is NSDictionary.Type:
return (object as! NSDictionary).description(withLocale: nil)
default:
if let hashableObject = object as? Dictionary<AnyHashable, Any> {
return hashableObject._nsObject.description(withLocale: nil, indent: 1)
} else {
return nil
}
}
}

open var descriptionInStringsFileFormat: String { NSUnimplemented() }
open var descriptionInStringsFileFormat: String {
var lines = [String]()
for key in self.allKeys {
let line = NSMutableString(capacity: 0)
line.append("\"")
if let descriptionByType = getDescription(of: key) {
line.append(descriptionByType)
} else {
line.append("\(key)")
}
line.append("\"")
line.append(" = ")
line.append("\"")
let value = self.object(forKey: key)!
if let descriptionByTypeValue = getDescription(of: value) {
line.append(descriptionByTypeValue)
} else {
line.append("\(value)")
}
line.append("\"")
line.append(";")
lines.append(line._bridgeToSwift())
}
return lines.joined(separator: "\n")
}

/// Returns a string object that represents the contents of the dictionary,
/// formatted as a property list.
Expand Down Expand Up @@ -315,11 +362,14 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
} else if object is NSSet {
line += (object as! NSSet).description(withLocale: locale)
} else {
line += "\(object)"
if let hashableObject = object as? Dictionary<AnyHashable, Any> {
line += hashableObject._nsObject.description(withLocale: nil, indent: level+1)
} else {
line += "\(object)"
}
}

line += ";"

lines.append(line)
}

Expand Down
5 changes: 2 additions & 3 deletions TestFoundation/TestNSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ class TestNSDictionary : XCTestCase {
func test_description() {
let d1: NSDictionary = [ "foo": "bar", "baz": "qux"]
XCTAssertEqual(d1.description, "{\n baz = qux;\n foo = bar;\n}")
let _: NSDictionary = ["1" : ["1" : ["1" : "1"]]]
// Disabled since it emits AnyHashable in the description for now...
// XCTAssertEqual(d2.description, "{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}")
let d2: NSDictionary = ["1" : ["1" : ["1" : "1"]]]
XCTAssertEqual(d2.description, "{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}")
}

func test_HeterogeneousConstruction() {
Expand Down