Skip to content

Implement NSSet#description #2037

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 4 commits into from
Mar 26, 2019
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
2 changes: 1 addition & 1 deletion Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ There is no _Complete_ status for test coverage because there are always additio
| `NSDictionary` | Mostly Complete | Incomplete | `NSCoding` with non-keyed-coding archivers, `descriptionInStringsFileFormat`, `sharedKeySet(forKeys:)`, and reading/writing to files/URLs remain unimplemented |
| `NSMutableDictionary` | Mostly Complete | Incomplete | `descriptionInStringsFileFormat`, `sharedKeySet(forKeys:)`, and reading/writing to files/URLs remain unimplemented |
| `NSCFDictionary` | N/A | N/A | For internal use only |
| `NSSet` | Mostly Complete | Incomplete | `description(withLocale:)` and `customMirror` remain unimplemented |
| `NSSet` | Mostly Complete | Incomplete | `customMirror` remain unimplemented |
| `NSMutableSet` | Mostly Complete | Incomplete | `init?(coder:)` remains unimplemented |
| `NSCountedSet` | Mostly Complete | Incomplete | `init?(coder:)` remains unimplemented |
| `NSCFSet` | N/A | N/A | For internal use only |
Expand Down
42 changes: 39 additions & 3 deletions Foundation/NSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,45 @@ open class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCodi
return true
}

open func description(withLocale locale: Locale?) -> String {
// NSUnimplemented()
return description
override open var description: String {
return description(withLocale: nil)
}

open func description(withLocale locale: Locale?) -> String {
return description(withLocale: locale, indent: 0)
}

private func description(withLocale locale: Locale?, indent level: Int) -> String {
var descriptions = [String]()

for obj in self._storage {
if let string = obj as? String {
descriptions.append(string)
} else if let array = obj as? [Any] {
descriptions.append(NSArray(array: array).description(withLocale: locale, indent: level + 1))
} else if let dict = obj as? [AnyHashable : Any] {
descriptions.append(dict._bridgeToObjectiveC().description(withLocale: locale, indent: level + 1))
} else if let set = obj as? Set<AnyHashable> {
descriptions.append(set._bridgeToObjectiveC().description(withLocale: locale, indent: level + 1))
} else {
descriptions.append("\(obj)")
}
}
var indent = ""
for _ in 0..<level {
indent += " "
}
var result = indent + "{(\n"
for idx in 0..<self.count {
result += indent + " " + descriptions[idx]
if idx + 1 < self.count {
result += ",\n"
} else {
result += "\n"
}
}
result += indent + ")}"
return result
}

override open var _cfTypeID: CFTypeID {
Expand Down
23 changes: 22 additions & 1 deletion TestFoundation/TestNSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class TestNSSet : XCTestCase {
("test_CountedSetRemoveObject", test_CountedSetRemoveObject),
("test_CountedSetCopying", test_CountedSetCopying),
("test_mutablesetWithDictionary", test_mutablesetWithDictionary),
("test_Subsets", test_Subsets)
("test_Subsets", test_Subsets),
("test_description", test_description)
]
}

Expand Down Expand Up @@ -237,4 +238,24 @@ class TestNSSet : XCTestCase {
XCTAssert(otherSet.isSubset(of: otherOtherSet))
XCTAssertFalse(newSet.isSubset(of: otherSet as! Set<AnyHashable>))
}

func test_description() {
let array = NSArray(array: ["array_element1", "array_element2"])
let dictionary = NSDictionary(dictionary: ["key1": "value1", "key2": "value2"])
let innerSet = NSSet(array: [4444, 5555])
let set: NSSet = NSSet(array: [array, dictionary, innerSet, 1111, 2222, 3333])

let description = NSString(string: set.description)

XCTAssertTrue(description.substring(to: 2).isEqual(to: "{("))
XCTAssertTrue(description.substring(from: description.length - 2).isEqual(to: ")}"))
XCTAssertTrue(description.contains(" (\n array_element1,\n array_element2\n )"))
XCTAssertTrue(description.contains(" key1 = value1"))
XCTAssertTrue(description.contains(" key2 = value2"))
XCTAssertTrue(description.contains(" 4444"))
XCTAssertTrue(description.contains(" 5555"))
XCTAssertTrue(description.contains(" 1111"))
XCTAssertTrue(description.contains(" 2222"))
XCTAssertTrue(description.contains(" 3333"))
}
}