Skip to content

Commit 30c1695

Browse files
committed
Foundation: add a custom AnyHashable representation for NSSet
1 parent 605218d commit 30c1695

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -921,23 +921,13 @@ extension Set : _ObjectiveCBridgeable {
921921
}
922922
}
923923

924-
/*
925-
FIXME(id-as-any): uncomment this when we can cast NSSet to Set<AnyHashable>.
926924
extension NSSet : _HasCustomAnyHashableRepresentation {
927925
// Must be @nonobjc to avoid infinite recursion during bridging
928926
@nonobjc
929927
public func _toCustomAnyHashable() -> AnyHashable? {
930-
var builder = _SetBuilder<Element>(count: s!.count)
931-
// FIXME(id-as-any): how to get the Hashable conformance here?
932-
s!.enumerateObjects({
933-
(anyMember: Any, stop: UnsafeMutablePointer<ObjCBool>) in
934-
builder.add(member: Swift._forceBridgeFromObjectiveC(
935-
anyMember as AnyObject, Element.self))
936-
})
937-
return AnyHashable(self as Set<AnyHashable>)
928+
return AnyHashable(self as! Set<AnyHashable>)
938929
}
939930
}
940-
*/
941931

942932
extension NSDictionary : Sequence {
943933
// FIXME: A class because we can't pass a struct with class fields through an

test/stdlib/NSSetAPI.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ NSSetAPI.test("CustomStringConvertible") {
4343
expectEqual(expect, result)
4444
}
4545

46+
NSSetAPI.test("AnyHashable containing NSSet") {
47+
let values: [NSSet] = [
48+
NSSet(),
49+
NSSet(objects: 1, 2, 3),
50+
NSSet(objects: 1, 2, 3),
51+
]
52+
let anyHashables = values.map(AnyHashable.init)
53+
expectEqual(Set<AnyHashable>.self, type(of: anyHashables[0].base))
54+
expectEqual(Set<AnyHashable>.self, type(of: anyHashables[1].base))
55+
expectEqual(Set<AnyHashable>.self, type(of: anyHashables[2].base))
56+
expectNotEqual(anyHashables[0], anyHashables[1])
57+
expectEqual(anyHashables[1], anyHashables[2])
58+
}
59+
60+
NSSetAPI.test("AnyHashable containing NSSet that contains an NSSet") {
61+
let anyHashable = AnyHashable(NSSet(objects: NSSet(objects: 1,2,3)))
62+
expectEqual(Set<AnyHashable>.self, type(of: anyHashable.base))
63+
64+
if let firstNested
65+
= expectNotNil((anyHashable.base as! Set<AnyHashable>).first!) {
66+
expectEqual(Set<AnyHashable>.self, type(of: firstNested.base))
67+
}
68+
}
69+
4670
var NSOrderedSetAPI = TestSuite("NSOrderedSetAPI")
4771

4872
NSOrderedSetAPI.test("Sequence") {

0 commit comments

Comments
 (0)