Skip to content

Commit 8057125

Browse files
committed
NSArray, NSDictionary: Perform elementwise AnyHashable conversion
1 parent aeeb25a commit 8057125

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Foundation/NSArray.swift

+11-3
Original file line numberDiff line numberDiff line change
@@ -936,9 +936,17 @@ open class NSMutableArray : NSArray {
936936
}
937937

938938
extension NSArray : _HasCustomAnyHashableRepresentation {
939-
public func _toCustomAnyHashable() -> AnyHashable? {
940-
return AnyHashable(self as! Array<AnyHashable>)
941-
}
939+
public func _toCustomAnyHashable() -> AnyHashable? {
940+
// FIXME: I want to return AnyHashable(self as! [AnyHashable])
941+
// here, but a direct conversion to AnyHashable can fail with nested
942+
// collections.
943+
var a: [AnyHashable] = []
944+
a.reserveCapacity(self.count)
945+
for member in self {
946+
a.append(member as AnyObject as! AnyHashable)
947+
}
948+
return AnyHashable(a)
949+
}
942950
}
943951

944952
extension NSArray : Sequence {

Foundation/NSDictionary.swift

+13-3
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,19 @@ extension Dictionary : _NSBridgeable, _CFBridgeable {
572572
}
573573

574574
extension NSDictionary : _HasCustomAnyHashableRepresentation {
575-
public func _toCustomAnyHashable() -> AnyHashable? {
576-
return AnyHashable(self as! Dictionary<AnyHashable, AnyHashable>)
577-
}
575+
public func _toCustomAnyHashable() -> AnyHashable? {
576+
// FIXME: I want to return AnyHashable(self as! [AnyHashable: AnyHashable])
577+
// here, but a direct conversion to AnyHashable can fail with nested
578+
// collections.
579+
var d: [AnyHashable: AnyHashable] = [:]
580+
d.reserveCapacity(self.count)
581+
for (key, value) in self {
582+
let k = key as AnyObject as! AnyHashable
583+
let v = value as AnyObject as! AnyHashable
584+
d[k] = v
585+
}
586+
return AnyHashable(d)
587+
}
578588
}
579589

580590
open class NSMutableDictionary : NSDictionary {

0 commit comments

Comments
 (0)