@@ -560,114 +560,26 @@ extension NSDictionary : ExpressibleByDictionaryLiteral {
560
560
}
561
561
}
562
562
563
- extension Dictionary {
564
- /// Private initializer used for bridging.
565
- ///
566
- /// The provided `NSDictionary` will be copied to ensure that the copy can
567
- /// not be mutated by other code.
568
- public init ( _cocoaDictionary: _NSDictionary ) {
569
- _sanityCheck (
570
- _isBridgedVerbatimToObjectiveC ( Key . self) &&
571
- _isBridgedVerbatimToObjectiveC ( Value . self) ,
572
- " Dictionary can be backed by NSDictionary storage only when both key and value are bridged verbatim to Objective-C " )
573
- // FIXME: We would like to call CFDictionaryCreateCopy() to avoid doing an
574
- // objc_msgSend() for instances of CoreFoundation types. We can't do that
575
- // today because CFDictionaryCreateCopy() copies dictionary contents
576
- // unconditionally, resulting in O(n) copies even for immutable dictionaries.
577
- //
578
- // <rdar://problem/20690755> CFDictionaryCreateCopy() does not call copyWithZone:
579
- //
580
- // The bug is fixed in: OS X 10.11.0, iOS 9.0, all versions of tvOS
581
- // and watchOS.
582
- self = Dictionary (
583
- _unsafeReferenceCast ( _cocoaDictionary, to: _NSDictionary. self) )
584
- }
585
- }
586
-
587
563
// Dictionary<Key, Value> is conditionally bridged to NSDictionary
588
- extension Dictionary : _ObjectiveCBridgeable {
564
+ extension Dictionary : _CustomObjectiveCBridgeable {
589
565
@_semantics ( " convertToObjectiveC " )
590
566
public func _bridgeToObjectiveC( ) -> NSDictionary {
591
567
return unsafeBitCast ( _bridgeToObjectiveCImpl ( ) as AnyObject ,
592
568
to: NSDictionary . self)
593
569
}
594
570
595
- public static func _forceBridgeFromObjectiveC(
596
- _ d: NSDictionary ,
597
- result: inout Dictionary ?
598
- ) {
599
- if let native = [ Key : Value ] . _bridgeFromObjectiveCAdoptingNativeStorageOf (
600
- d as AnyObject ) {
601
- result = native
602
- return
603
- }
604
-
605
- if _isBridgedVerbatimToObjectiveC ( Key . self) &&
606
- _isBridgedVerbatimToObjectiveC ( Value . self) {
607
- result = [ Key : Value] (
608
- _cocoaDictionary: unsafeBitCast ( d as AnyObject , to: _NSDictionary. self) )
609
- return
610
- }
611
-
612
- // `Dictionary<Key, Value>` where either `Key` or `Value` is a value type
613
- // may not be backed by an NSDictionary.
614
- var builder = _DictionaryBuilder < Key , Value > ( count: d. count)
615
- d. enumerateKeysAndObjects ( {
616
- ( anyKey: Any , anyValue: Any ,
617
- stop: UnsafeMutablePointer < ObjCBool > ) in
618
- let anyObjectKey = anyKey as AnyObject
619
- let anyObjectValue = anyValue as AnyObject
620
- builder. add (
621
- key: Swift . _forceBridgeFromObjectiveC ( anyObjectKey, Key . self) ,
622
- value: Swift . _forceBridgeFromObjectiveC ( anyObjectValue, Value . self) )
623
- } )
624
- result = builder. take ( )
625
- }
626
-
627
- public static func _conditionallyBridgeFromObjectiveC(
628
- _ x: NSDictionary ,
629
- result: inout Dictionary ?
630
- ) -> Bool {
631
- let anyDict = x as [ NSObject : AnyObject ]
632
- if _isBridgedVerbatimToObjectiveC ( Key . self) &&
633
- _isBridgedVerbatimToObjectiveC ( Value . self) {
634
- result = Swift . _dictionaryDownCastConditional ( anyDict)
635
- return result != nil
636
- }
637
-
638
- result = Swift . _dictionaryBridgeFromObjectiveCConditional ( anyDict)
639
- return result != nil
571
+ public static func _bridgedFromNil( _: NSDictionary ? ) -> Dictionary {
572
+ return [ : ]
640
573
}
641
-
642
- public static func _unconditionallyBridgeFromObjectiveC(
643
- _ d: NSDictionary ?
644
- ) -> Dictionary {
645
- // `nil` has historically been used as a stand-in for an empty
646
- // dictionary; map it to an empty dictionary.
647
- if _slowPath ( d == nil ) { return Dictionary ( ) }
648
-
649
- if let native = [ Key : Value ] . _bridgeFromObjectiveCAdoptingNativeStorageOf (
650
- d! as AnyObject ) {
651
- return native
574
+
575
+ public static func _bridged< Policy: _BridgePolicy > (
576
+ from objc: NSDictionary , by policy: Policy . Type
577
+ ) -> Dictionary ? {
578
+ if let n = Dictionary . _bridgeFromObjectiveCAdoptingNativeStorageOf ( objc) {
579
+ return n
652
580
}
653
-
654
- if _isBridgedVerbatimToObjectiveC ( Key . self) &&
655
- _isBridgedVerbatimToObjectiveC ( Value . self) {
656
- return [ Key : Value] (
657
- _cocoaDictionary: unsafeBitCast ( d! as AnyObject , to: _NSDictionary. self) )
658
- }
659
-
660
- // `Dictionary<Key, Value>` where either `Key` or `Value` is a value type
661
- // may not be backed by an NSDictionary.
662
- var builder = _DictionaryBuilder < Key , Value > ( count: d!. count)
663
- d!. enumerateKeysAndObjects ( {
664
- ( anyKey: Any , anyValue: Any ,
665
- stop: UnsafeMutablePointer < ObjCBool > ) in
666
- builder. add (
667
- key: Swift . _forceBridgeFromObjectiveC ( anyKey as AnyObject , Key . self) ,
668
- value: Swift . _forceBridgeFromObjectiveC ( anyValue as AnyObject , Value . self) )
669
- } )
670
- return builder. take ( )
581
+ return Dictionary (
582
+ _unsafeReferenceCast ( objc, to: _NSDictionary. self) , by: policy)
671
583
}
672
584
}
673
585
0 commit comments