Skip to content

Commit a6cc024

Browse files
committed
Merge pull request #114 from thii/nsdictionary-description
[NSDictionary] Implement description, descriptionWithLocale, descriptionWithLocale:indent and tests
2 parents 1b70fd2 + 0af5f45 commit a6cc024

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

Foundation/NSDictionary.swift

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,101 @@ public class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCodin
264264
return matching
265265
}
266266

267-
public override var description: String { NSUnimplemented() }
267+
/// A string that represents the contents of the dictionary, formatted as
268+
/// a property list (read-only)
269+
///
270+
/// If each key in the dictionary is an NSString object, the entries are
271+
/// listed in ascending order by key, otherwise the order in which the entries
272+
/// are listed is undefined. This property is intended to produce readable
273+
/// output for debugging purposes, not for serializing data. If you want to
274+
/// store dictionary data for later retrieval, see
275+
/// [Property List Programming Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html#//apple_ref/doc/uid/10000048i)
276+
/// and [Archives and Serializations Programming Guide](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i).
277+
public override var description: String {
278+
get {
279+
return descriptionWithLocale(nil)
280+
}
281+
}
282+
268283
public var descriptionInStringsFileFormat: String { NSUnimplemented() }
269-
public func descriptionWithLocale(locale: AnyObject?) -> String { NSUnimplemented() }
270-
public func descriptionWithLocale(locale: AnyObject?, indent level: Int) -> String { NSUnimplemented() }
271-
284+
285+
/// Returns a string object that represents the contents of the dictionary,
286+
/// formatted as a property list.
287+
///
288+
/// - parameter locale: An object that specifies options used for formatting
289+
/// each of the dictionary’s keys and values; pass `nil` if you don’t
290+
/// want them formatted.
291+
public func descriptionWithLocale(locale: AnyObject?) -> String {
292+
return descriptionWithLocale(locale, indent: 0)
293+
}
294+
295+
/// Returns a string object that represents the contents of the dictionary,
296+
/// formatted as a property list.
297+
///
298+
/// - parameter locale: An object that specifies options used for formatting
299+
/// each of the dictionary’s keys and values; pass `nil` if you don’t
300+
/// want them formatted.
301+
///
302+
/// - parameter level: Specifies a level of indentation, to make the output
303+
/// more readable: the indentation is (4 spaces) * level.
304+
///
305+
/// - returns: A string object that represents the contents of the dictionary,
306+
/// formatted as a property list.
307+
public func descriptionWithLocale(locale: AnyObject?, indent level: Int) -> String {
308+
if level > 100 { return "..." }
309+
310+
var lines = [String]()
311+
let indentation = String(count: level * 4, repeatedValue: Character(" "))
312+
lines.append(indentation + "{")
313+
314+
for key in self.allKeys {
315+
var line = String(count: (level + 1) * 4, repeatedValue: Character(" "))
316+
317+
if key is NSArray {
318+
line += (key as! NSArray).descriptionWithLocale(locale, indent: level + 1)
319+
} else if key is NSDate {
320+
line += (key as! NSDate).descriptionWithLocale(locale)
321+
} else if key is NSDecimalNumber {
322+
line += (key as! NSDecimalNumber).descriptionWithLocale(locale)
323+
} else if key is NSDictionary {
324+
line += (key as! NSDictionary).descriptionWithLocale(locale, indent: level + 1)
325+
} else if key is NSOrderedSet {
326+
line += (key as! NSOrderedSet).descriptionWithLocale(locale, indent: level + 1)
327+
} else if key is NSSet {
328+
line += (key as! NSSet).descriptionWithLocale(locale)
329+
} else {
330+
line += "\(key)"
331+
}
332+
333+
line += " = "
334+
335+
let object = objectForKey(key)!
336+
if object is NSArray {
337+
line += (object as! NSArray).descriptionWithLocale(locale, indent: level + 1)
338+
} else if object is NSDate {
339+
line += (object as! NSDate).descriptionWithLocale(locale)
340+
} else if object is NSDecimalNumber {
341+
line += (object as! NSDecimalNumber).descriptionWithLocale(locale)
342+
} else if object is NSDictionary {
343+
line += (object as! NSDictionary).descriptionWithLocale(locale, indent: level + 1)
344+
} else if object is NSOrderedSet {
345+
line += (object as! NSOrderedSet).descriptionWithLocale(locale, indent: level + 1)
346+
} else if object is NSSet {
347+
line += (object as! NSSet).descriptionWithLocale(locale)
348+
} else {
349+
line += "\(object)"
350+
}
351+
352+
line += ";"
353+
354+
lines.append(line)
355+
}
356+
357+
lines.append(indentation + "}")
358+
359+
return lines.joinWithSeparator("\n")
360+
}
361+
272362
public func isEqualToDictionary(otherDictionary: [NSObject : AnyObject]) -> Bool {
273363
if count != otherDictionary.count {
274364
return false

TestFoundation/TestNSDictionary.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestNSDictionary : XCTestCase {
2525
return [
2626
("test_BasicConstruction", test_BasicConstruction),
2727
("test_ArrayConstruction", test_ArrayConstruction),
28+
("test_description", test_description),
2829
("test_enumeration", test_enumeration),
2930
]
3031
}
@@ -36,6 +37,15 @@ class TestNSDictionary : XCTestCase {
3637
XCTAssertEqual(dict2.count, 1)
3738
}
3839

40+
41+
func test_description() {
42+
let d1: NSDictionary = [ "foo": "bar", "baz": "qux"].bridge()
43+
XCTAssertEqual(d1.description, "{\n baz = qux;\n foo = bar;\n}")
44+
45+
let d2: NSDictionary = ["1" : ["1" : ["1" : "1"]]].bridge()
46+
XCTAssertEqual(d2.description, "{\n 1 = {\n 1 = {\n 1 = 1;\n };\n };\n}")
47+
}
48+
3949
func test_HeterogeneousConstruction() {
4050
// let dict2: NSDictionary = [
4151
// "foo": "bar",

0 commit comments

Comments
 (0)