Skip to content

Foundation: add a custom AnyHashable representation for NSSet #4810

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 15, 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
12 changes: 1 addition & 11 deletions stdlib/public/SDK/Foundation/Foundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -921,23 +921,13 @@ extension Set : _ObjectiveCBridgeable {
}
}

/*
FIXME(id-as-any): uncomment this when we can cast NSSet to Set<AnyHashable>.
extension NSSet : _HasCustomAnyHashableRepresentation {
// Must be @nonobjc to avoid infinite recursion during bridging
@nonobjc
public func _toCustomAnyHashable() -> AnyHashable? {
var builder = _SetBuilder<Element>(count: s!.count)
// FIXME(id-as-any): how to get the Hashable conformance here?
s!.enumerateObjects({
(anyMember: Any, stop: UnsafeMutablePointer<ObjCBool>) in
builder.add(member: Swift._forceBridgeFromObjectiveC(
anyMember as AnyObject, Element.self))
})
return AnyHashable(self as Set<AnyHashable>)
return AnyHashable(self as! Set<AnyHashable>)
}
}
*/

extension NSDictionary : Sequence {
// FIXME: A class because we can't pass a struct with class fields through an
Expand Down
24 changes: 24 additions & 0 deletions test/stdlib/NSSetAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ NSSetAPI.test("CustomStringConvertible") {
expectEqual(expect, result)
}

NSSetAPI.test("AnyHashable containing NSSet") {
let values: [NSSet] = [
NSSet(),
NSSet(objects: 1, 2, 3),
NSSet(objects: 1, 2, 3),
]
let anyHashables = values.map(AnyHashable.init)
expectEqual(Set<AnyHashable>.self, type(of: anyHashables[0].base))
expectEqual(Set<AnyHashable>.self, type(of: anyHashables[1].base))
expectEqual(Set<AnyHashable>.self, type(of: anyHashables[2].base))
expectNotEqual(anyHashables[0], anyHashables[1])
expectEqual(anyHashables[1], anyHashables[2])
}

NSSetAPI.test("AnyHashable containing NSSet that contains an NSSet") {
let anyHashable = AnyHashable(NSSet(objects: NSSet(objects: 1,2,3)))
expectEqual(Set<AnyHashable>.self, type(of: anyHashable.base))

if let firstNested
= expectNotNil((anyHashable.base as! Set<AnyHashable>).first!) {
expectEqual(Set<AnyHashable>.self, type(of: firstNested.base))
}
}

var NSOrderedSetAPI = TestSuite("NSOrderedSetAPI")

NSOrderedSetAPI.test("Sequence") {
Expand Down