Skip to content

Commit 0856854

Browse files
committed
[Foundation] Implement NSSet#description
Because `NSSet#description` was not implemented.
1 parent 0c82d32 commit 0856854

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

Docs/Status.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ There is no _Complete_ status for test coverage because there are always additio
166166
| `NSDictionary` | Mostly Complete | Incomplete | `NSCoding` with non-keyed-coding archivers, `descriptionInStringsFileFormat`, `sharedKeySet(forKeys:)`, and reading/writing to files/URLs remain unimplemented |
167167
| `NSMutableDictionary` | Mostly Complete | Incomplete | `descriptionInStringsFileFormat`, `sharedKeySet(forKeys:)`, and reading/writing to files/URLs remain unimplemented |
168168
| `NSCFDictionary` | N/A | N/A | For internal use only |
169-
| `NSSet` | Mostly Complete | Incomplete | `description(withLocale:)` and `customMirror` remain unimplemented |
169+
| `NSSet` | Mostly Complete | Incomplete | `customMirror` remain unimplemented |
170170
| `NSMutableSet` | Mostly Complete | Incomplete | `init?(coder:)` remains unimplemented |
171171
| `NSCountedSet` | Mostly Complete | Incomplete | `init?(coder:)` remains unimplemented |
172172
| `NSCFSet` | N/A | N/A | For internal use only |

Foundation/NSSet.swift

+40-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,46 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
107107
return true
108108
}
109109

110-
open func description(withLocale locale: Locale?) -> String {
111-
// NSUnimplemented()
112-
return description
110+
override open var description: String {
111+
return description(withLocale: nil)
112+
}
113+
114+
open func description(withLocale locale: Locale?) -> String {
115+
return description(withLocale: locale, indent: 0)
116+
}
117+
118+
private func description(withLocale locale: Locale?, indent level: Int) -> String {
119+
var descriptions = [String]()
120+
121+
for obj in self._storage {
122+
if let string = obj as? String {
123+
descriptions.append(string)
124+
} else if let array = obj as? [Any] {
125+
descriptions.append(NSArray(array: array).description(withLocale: locale, indent: level + 1))
126+
} else if let dict = obj as? [AnyHashable : Any] {
127+
descriptions.append(dict._bridgeToObjectiveC().description(withLocale: locale, indent: level + 1))
128+
} else if let set = obj as? Set<AnyHashable> {
129+
descriptions.append(set._bridgeToObjectiveC().description(withLocale: locale, indent: level + 1))
130+
} else {
131+
descriptions.append("\(obj)")
132+
}
133+
}
134+
var indent = ""
135+
for _ in 0..<level {
136+
indent += " "
137+
}
138+
var result = indent + "{(\n"
139+
let cnt = count
140+
for idx in 0..<cnt {
141+
result += indent + " " + descriptions[idx]
142+
if idx + 1 < cnt {
143+
result += ",\n"
144+
} else {
145+
result += "\n"
146+
}
147+
}
148+
result += indent + ")}"
149+
return result
113150
}
114151

115152
override open var _cfTypeID: CFTypeID {

TestFoundation/TestNSSet.swift

+22-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class TestNSSet : XCTestCase {
2525
("test_CountedSetRemoveObject", test_CountedSetRemoveObject),
2626
("test_CountedSetCopying", test_CountedSetCopying),
2727
("test_mutablesetWithDictionary", test_mutablesetWithDictionary),
28-
("test_Subsets", test_Subsets)
28+
("test_Subsets", test_Subsets),
29+
("test_description", test_description)
2930
]
3031
}
3132

@@ -237,4 +238,24 @@ class TestNSSet : XCTestCase {
237238
XCTAssert(otherSet.isSubset(of: otherOtherSet))
238239
XCTAssertFalse(newSet.isSubset(of: otherSet as! Set<AnyHashable>))
239240
}
241+
242+
func test_description() {
243+
let array = NSArray(array: ["array_element1", "array_element2"])
244+
let dictionary = NSDictionary(dictionary: ["key1": "value1", "key2": "value2"])
245+
let innerSet = NSSet(array: [4444, 5555])
246+
let set: NSSet = NSSet(array: [array, dictionary, innerSet, 1111, 2222, 3333])
247+
248+
let description = set.description
249+
250+
XCTAssertTrue(String(description.prefix(2)) == "{(")
251+
XCTAssertTrue(String(description.suffix(2)) == ")}")
252+
XCTAssertTrue(description.contains(" (\n array_element1,\n array_element2\n )"))
253+
XCTAssertTrue(description.contains(" key1 = value1"))
254+
XCTAssertTrue(description.contains(" key2 = value2"))
255+
XCTAssertTrue(description.contains(" 4444"))
256+
XCTAssertTrue(description.contains(" 5555"))
257+
XCTAssertTrue(description.contains(" 1111"))
258+
XCTAssertTrue(description.contains(" 2222"))
259+
XCTAssertTrue(description.contains(" 3333"))
260+
}
240261
}

0 commit comments

Comments
 (0)