Skip to content

Commit 43e34d4

Browse files
committed
NSDictionary: Add customMirror and missing init() method.
- Add init(dictionary:copyItems:). - Add .customMirror - ExpressibleByDictionaryLiteral: init(dictionaryLiteral: (Any, Any)...) already existed, just add the conformance to the class declaration. - Group the init() methods together.
1 parent 2ab5621 commit 43e34d4

File tree

1 file changed

+50
-48
lines changed

1 file changed

+50
-48
lines changed

Foundation/NSDictionary.swift

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import CoreFoundation
1212
import Dispatch
1313

14-
open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCoding {
14+
open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCoding, ExpressibleByDictionaryLiteral {
1515
private let _cfinfo = _CFInfo(typeID: CFDictionaryGetTypeID())
1616
internal var _storage: [NSObject: AnyObject]
1717

@@ -38,7 +38,6 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
3838
} else {
3939
return object(forKey: key)
4040
}
41-
4241
}
4342

4443
open func keyEnumerator() -> NSEnumerator {
@@ -78,7 +77,50 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
7877
_storage[key as! NSObject] = value
7978
}
8079
}
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+
82124
public required convenience init?(coder aDecoder: NSCoder) {
83125
guard aDecoder.allowsKeyedCoding else {
84126
preconditionFailure("Unkeyed coding is unsupported.")
@@ -144,29 +186,6 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
144186
return NSMutableDictionary(objects: self.allValues, forKeys: self.allKeys.map { __SwiftValue.store($0) } )
145187
}
146188

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-
170189
open override func isEqual(_ value: Any?) -> Bool {
171190
switch value {
172191
case let other as Dictionary<AnyHashable, Any>:
@@ -543,18 +562,6 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
543562
override open var _cfTypeID: CFTypeID {
544563
return CFDictionaryGetTypeID()
545564
}
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-
}
558565
}
559566

560567
extension NSDictionary : _CFBridgeable, _SwiftBridgeable {
@@ -608,11 +615,7 @@ open class NSMutableDictionary : NSDictionary {
608615
public required init(objects: UnsafePointer<AnyObject>!, forKeys keys: UnsafePointer<NSObject>!, count cnt: Int) {
609616
super.init(objects: objects, forKeys: keys, count: cnt)
610617
}
611-
612-
}
613618

614-
extension NSMutableDictionary {
615-
616619
open func addEntries(from otherDictionary: [AnyHashable : Any]) {
617620
for (key, obj) in otherDictionary {
618621
setObject(obj, forKey: key)
@@ -690,14 +693,13 @@ extension NSMutableDictionary {
690693
public convenience init(sharedKeySet keyset: Any) { NSUnimplemented() }
691694
}
692695

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+
}
697700
}
698701

699-
700-
extension NSDictionary : _StructTypeBridgeable {
702+
extension NSDictionary: _StructTypeBridgeable {
701703
public typealias _StructType = Dictionary<AnyHashable,Any>
702704

703705
public func _bridgeToSwift() -> _StructType {

0 commit comments

Comments
 (0)