Skip to content

Try letting NSDictionary duck-typecheck things instead of doing it ourselves the slow way #26194

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
Jul 18, 2019
Merged
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
15 changes: 10 additions & 5 deletions stdlib/public/Darwin/Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
@_exported import Foundation // Clang module
import _SwiftFoundationOverlayShims

// We don't check for NSCopying here for performance reasons. We would
// just crash anyway, and NSMutableDictionary will still do that when
// it tries to call -copyWithZone: and it's not there
private func duckCastToNSCopying(_ x: Any) -> NSCopying {
return _unsafeReferenceCast(x as AnyObject, to: NSCopying.self)
}

//===----------------------------------------------------------------------===//
// Dictionaries
//===----------------------------------------------------------------------===//
Expand All @@ -21,10 +28,10 @@ extension NSDictionary : ExpressibleByDictionaryLiteral {
public required convenience init(
dictionaryLiteral elements: (Any, Any)...
) {
// FIXME: Unfortunate that the `NSCopying` check has to be done at runtime.

self.init(
objects: elements.map { $0.1 as AnyObject },
forKeys: elements.map { $0.0 as AnyObject as! NSCopying },
forKeys: elements.map { duckCastToNSCopying($0.0) },
count: elements.count)
}
}
Expand Down Expand Up @@ -401,9 +408,7 @@ extension NSMutableDictionary {
}
@objc(__swift_setObject:forKeyedSubscript:)
set {
// FIXME: Unfortunate that the `NSCopying` check has to be done at
// runtime.
let copyingKey = key as AnyObject as! NSCopying
let copyingKey = duckCastToNSCopying(key)
if let newValue = newValue {
self.setObject(newValue, forKey: copyingKey)
} else {
Expand Down