- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathDictionary.swift
97 lines (81 loc) · 3.59 KB
/
Dictionary.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
import CoreFoundation
extensionDictionary:_ObjectiveCBridgeable{
publictypealias_ObjectType=NSDictionary
publicfunc _bridgeToObjectiveC()->_ObjectType{
letkeyBuffer= UnsafeMutablePointer<NSObject>.allocate(capacity: count)
letvalueBuffer= UnsafeMutablePointer<AnyObject>.allocate(capacity: count)
varidx=0
self.forEach{(keyItem, valueItem)in
letkey= _SwiftValue.store(keyItem)
letvalue= _SwiftValue.store(valueItem)
keyBuffer.advanced(by: idx).initialize(to: key)
valueBuffer.advanced(by: idx).initialize(to: value)
idx +=1
}
letdict=NSDictionary(objects: valueBuffer, forKeys: keyBuffer, count: count)
keyBuffer.deinitialize(count: count)
valueBuffer.deinitialize(count: count)
keyBuffer.deallocate()
valueBuffer.deallocate()
return dict
}
staticpublicfunc _forceBridgeFromObjectiveC(_ source:_ObjectType, result:inoutDictionary?){
result =_unconditionallyBridgeFromObjectiveC(source)
}
@discardableResult
staticpublicfunc _conditionallyBridgeFromObjectiveC(_ source:_ObjectType, result:inoutDictionary?)->Bool{
vardict=[Key: Value]()
varfailedConversion=false
iftype(of: source)==NSDictionary.self || type(of: source)==NSMutableDictionary.self {
source.enumerateKeysAndObjects(options:[]){ key, value, stop in
guardlet key = key as?Key,let value = value as?Valueelse{
failedConversion =true
stop.pointee =true
return
}
dict[key]= value
}
}elseiftype(of: source)== _NSCFDictionary.self {
letcf= source._cfObject
letcnt=CFDictionaryGetCount(cf)
letkeys= UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: cnt)
letvalues= UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: cnt)
CFDictionaryGetKeysAndValues(cf, keys, values)
foridxin0..<cnt {
letkey= _SwiftValue.fetch(nonOptional:unsafeBitCast(keys.advanced(by: idx).pointee!, to:AnyObject.self))
letvalue= _SwiftValue.fetch(nonOptional:unsafeBitCast(values.advanced(by: idx).pointee!, to:AnyObject.self))
guardlet k = key as?Key,let v = value as?Valueelse{
failedConversion =true
break
}
dict[k]= v
}
keys.deinitialize(count: cnt)
values.deinitialize(count: cnt)
keys.deallocate()
values.deallocate()
}
if !failedConversion {
result = dict
returntrue
}
returnfalse
}
staticpublicfunc _unconditionallyBridgeFromObjectiveC(_ source:_ObjectType?)->Dictionary{
iflet object = source {
varvalue:Dictionary<Key,Value>?
_conditionallyBridgeFromObjectiveC(object, result:&value)
return value!
}else{
returnDictionary<Key,Value>()
}
}
}