Skip to content

Commit 5e83f80

Browse files
committed
Let NSMutableDictionary check for -copy on keys, rather than doing it ourselves with a full (slow) conformance check
1 parent 37db587 commit 5e83f80

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

stdlib/public/Darwin/Foundation/NSDictionary.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
@_exported import Foundation // Clang module
1414
import _SwiftFoundationOverlayShims
1515

16+
// We don't check for NSCopying here for performance reasons. We would
17+
// just crash anyway, and NSMutableDictionary will still do that when
18+
// it tries to call -copyWithZone: and it's not there
19+
private func duckCastToNSCopying(_ x: Any) -> NSCopying {
20+
return _unsafeReferenceCast(x as AnyObject, to: NSCopying.self)
21+
}
22+
1623
//===----------------------------------------------------------------------===//
1724
// Dictionaries
1825
//===----------------------------------------------------------------------===//
@@ -21,10 +28,10 @@ extension NSDictionary : ExpressibleByDictionaryLiteral {
2128
public required convenience init(
2229
dictionaryLiteral elements: (Any, Any)...
2330
) {
24-
// FIXME: Unfortunate that the `NSCopying` check has to be done at runtime.
31+
2532
self.init(
2633
objects: elements.map { $0.1 as AnyObject },
27-
forKeys: elements.map { $0.0 as AnyObject as! NSCopying },
34+
forKeys: elements.map { duckCastToNSCopying($0.0) },
2835
count: elements.count)
2936
}
3037
}
@@ -401,9 +408,7 @@ extension NSMutableDictionary {
401408
}
402409
@objc(__swift_setObject:forKeyedSubscript:)
403410
set {
404-
// FIXME: Unfortunate that the `NSCopying` check has to be done at
405-
// runtime.
406-
let copyingKey = key as AnyObject as! NSCopying
411+
let copyingKey = duckCastToNSCopying(key)
407412
if let newValue = newValue {
408413
self.setObject(newValue, forKey: copyingKey)
409414
} else {

0 commit comments

Comments
 (0)