11
11
import CoreFoundation
12
12
import Dispatch
13
13
14
- open class NSDictionary : NSObject , NSCopying , NSMutableCopying , NSSecureCoding , NSCoding {
14
+ open class NSDictionary : NSObject , NSCopying , NSMutableCopying , NSSecureCoding , NSCoding , ExpressibleByDictionaryLiteral {
15
15
private let _cfinfo = _CFInfo ( typeID: CFDictionaryGetTypeID ( ) )
16
16
internal var _storage : [ NSObject : AnyObject ]
17
17
@@ -38,7 +38,6 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
38
38
} else {
39
39
return object ( forKey: key)
40
40
}
41
-
42
41
}
43
42
44
43
open func keyEnumerator( ) -> NSEnumerator {
@@ -78,7 +77,50 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
78
77
_storage [ key as! NSObject ] = value
79
78
}
80
79
}
81
-
80
+
81
+ public convenience init ( object: Any , forKey key: NSCopying ) {
82
+ self . init ( objects: [ object] , forKeys: [ key as! NSObject ] )
83
+ }
84
+
85
+ public convenience init ( objects: [ Any ] , forKeys keys: [ NSObject ] ) {
86
+ let keyBuffer = UnsafeMutablePointer< NSObject> . allocate( capacity: keys. count)
87
+ keyBuffer. initialize ( from: keys, count: keys. count)
88
+
89
+ let valueBuffer = UnsafeMutablePointer< AnyObject> . allocate( capacity: objects. count)
90
+ valueBuffer. initialize ( from: objects. map { __SwiftValue. store ( $0) } , count: objects. count)
91
+
92
+ self . init ( objects: valueBuffer, forKeys: keyBuffer, count: keys. count)
93
+
94
+ keyBuffer. deinitialize ( count: keys. count)
95
+ valueBuffer. deinitialize ( count: objects. count)
96
+ keyBuffer. deallocate ( )
97
+ valueBuffer. deallocate ( )
98
+ }
99
+
100
+ public convenience init ( dictionary otherDictionary: [ AnyHashable : Any ] ) {
101
+ self . init ( dictionary: otherDictionary, copyItems: false )
102
+ }
103
+
104
+ public convenience init ( dictionary otherDictionary: [ AnyHashable : Any ] , copyItems flag: Bool ) {
105
+ if flag {
106
+ self . init ( objects: Array ( otherDictionary. values. map { __SwiftValue ( $0) . copy ( ) as! NSObject } ) , forKeys: otherDictionary. keys. map { __SwiftValue. store ( $0) . copy ( ) as! NSObject } )
107
+ } else {
108
+ self . init ( objects: Array ( otherDictionary. values) , forKeys: otherDictionary. keys. map { __SwiftValue. store ( $0) } )
109
+ }
110
+ }
111
+
112
+ required public convenience init ( dictionaryLiteral elements: ( Any , Any ) ... ) {
113
+ var keys = [ NSObject] ( )
114
+ var values = [ Any] ( )
115
+
116
+ for (key, value) in elements {
117
+ keys. append ( __SwiftValue. store ( key) )
118
+ values. append ( value)
119
+ }
120
+
121
+ self . init ( objects: values, forKeys: keys)
122
+ }
123
+
82
124
public required convenience init ? ( coder aDecoder: NSCoder ) {
83
125
guard aDecoder. allowsKeyedCoding else {
84
126
preconditionFailure ( " Unkeyed coding is unsupported. " )
@@ -144,29 +186,6 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
144
186
return NSMutableDictionary ( objects: self . allValues, forKeys: self . allKeys. map { __SwiftValue. store ( $0) } )
145
187
}
146
188
147
- public convenience init ( object: Any , forKey key: NSCopying ) {
148
- self . init ( objects: [ object] , forKeys: [ key as! NSObject ] )
149
- }
150
-
151
- public convenience init ( objects: [ Any ] , forKeys keys: [ NSObject ] ) {
152
- let keyBuffer = UnsafeMutablePointer< NSObject> . allocate( capacity: keys. count)
153
- keyBuffer. initialize ( from: keys, count: keys. count)
154
-
155
- let valueBuffer = UnsafeMutablePointer< AnyObject> . allocate( capacity: objects. count)
156
- valueBuffer. initialize ( from: objects. map { __SwiftValue. store ( $0) } , count: objects. count)
157
-
158
- self . init ( objects: valueBuffer, forKeys: keyBuffer, count: keys. count)
159
-
160
- keyBuffer. deinitialize ( count: keys. count)
161
- valueBuffer. deinitialize ( count: objects. count)
162
- keyBuffer. deallocate ( )
163
- valueBuffer. deallocate ( )
164
- }
165
-
166
- public convenience init ( dictionary otherDictionary: [ AnyHashable : Any ] ) {
167
- self . init ( objects: Array ( otherDictionary. values) , forKeys: otherDictionary. keys. map { __SwiftValue. store ( $0) } )
168
- }
169
-
170
189
open override func isEqual( _ value: Any ? ) -> Bool {
171
190
switch value {
172
191
case let other as Dictionary < AnyHashable , Any > :
@@ -543,18 +562,6 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
543
562
override open var _cfTypeID : CFTypeID {
544
563
return CFDictionaryGetTypeID ( )
545
564
}
546
-
547
- required public convenience init ( dictionaryLiteral elements: ( Any , Any ) ... ) {
548
- var keys = [ NSObject] ( )
549
- var values = [ Any] ( )
550
-
551
- for (key, value) in elements {
552
- keys. append ( __SwiftValue. store ( key) )
553
- values. append ( value)
554
- }
555
-
556
- self . init ( objects: values, forKeys: keys)
557
- }
558
565
}
559
566
560
567
extension NSDictionary : _CFBridgeable , _SwiftBridgeable {
@@ -608,11 +615,7 @@ open class NSMutableDictionary : NSDictionary {
608
615
public required init ( objects: UnsafePointer < AnyObject > ! , forKeys keys: UnsafePointer < NSObject > ! , count cnt: Int ) {
609
616
super. init ( objects: objects, forKeys: keys, count: cnt)
610
617
}
611
-
612
- }
613
618
614
- extension NSMutableDictionary {
615
-
616
619
open func addEntries( from otherDictionary: [ AnyHashable : Any ] ) {
617
620
for (key, obj) in otherDictionary {
618
621
setObject ( obj, forKey: key)
@@ -690,14 +693,13 @@ extension NSMutableDictionary {
690
693
public convenience init ( sharedKeySet keyset: Any ) { NSUnimplemented ( ) }
691
694
}
692
695
693
- extension NSDictionary : ExpressibleByDictionaryLiteral { }
694
-
695
- extension NSDictionary : CustomReflectable {
696
- public var customMirror : Mirror { NSUnimplemented ( ) }
696
+ extension NSDictionary : CustomReflectable {
697
+ public var customMirror : Mirror {
698
+ return Mirror ( reflecting : self . _storage as [ NSObject : AnyObject ] )
699
+ }
697
700
}
698
701
699
-
700
- extension NSDictionary : _StructTypeBridgeable {
702
+ extension NSDictionary : _StructTypeBridgeable {
701
703
public typealias _StructType = Dictionary < AnyHashable , Any >
702
704
703
705
public func _bridgeToSwift( ) -> _StructType {
0 commit comments