13
13
@_exported import Foundation // Clang module
14
14
import _SwiftFoundationOverlayShims
15
15
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
+
16
23
//===----------------------------------------------------------------------===//
17
24
// Dictionaries
18
25
//===----------------------------------------------------------------------===//
@@ -21,10 +28,10 @@ extension NSDictionary : ExpressibleByDictionaryLiteral {
21
28
public required convenience init (
22
29
dictionaryLiteral elements: ( Any , Any ) ...
23
30
) {
24
- // FIXME: Unfortunate that the `NSCopying` check has to be done at runtime.
31
+
25
32
self . init (
26
33
objects: elements. map { $0. 1 as AnyObject } ,
27
- forKeys: elements. map { $0. 0 as AnyObject as! NSCopying } ,
34
+ forKeys: elements. map { duckCastToNSCopying ( $0. 0 ) } ,
28
35
count: elements. count)
29
36
}
30
37
}
@@ -401,9 +408,7 @@ extension NSMutableDictionary {
401
408
}
402
409
@objc ( __swift_setObject: forKeyedSubscript: )
403
410
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)
407
412
if let newValue = newValue {
408
413
self . setObject ( newValue, forKey: copyingKey)
409
414
} else {
0 commit comments