13
13
@_exported import Foundation // Clang module
14
14
15
15
16
- @_silgen_name ( " __NSNotificationCreate " )
17
- internal func __NSNotificationCreate( _ name: NSString , _ object: AnyObject ? , _ userInfo: AnyObject ? ) -> NSNotification
18
-
19
- @_silgen_name ( " __NSNotificationUserInfo " )
20
- internal func __NSNotificationUserInfo( _ notif: NSNotification ) -> AnyObject ?
21
-
22
16
/**
23
17
`Notification` encapsulates information broadcast to observers via a `NotificationCenter`.
24
18
*/
@@ -34,12 +28,12 @@ public struct Notification : ReferenceConvertible, Equatable, Hashable {
34
28
public var object : Any ?
35
29
36
30
/// Storage for values or objects related to this notification.
37
- public var userInfo : [ String : Any ] ?
31
+ public var userInfo : [ AnyHashable : Any ] ?
38
32
39
33
/// Initialize a new `Notification`.
40
34
///
41
35
/// The default value for `userInfo` is nil.
42
- public init( name: Name, object: Any? = nil , userInfo: [ String : Any] ? = nil ) {
36
+ public init ( name: Name , object: Any ? = nil , userInfo: [ AnyHashable : Any ] ? = nil ) {
43
37
self . name = name
44
38
self . object = object
45
39
self . userInfo = userInfo
@@ -61,8 +55,6 @@ public struct Notification : ReferenceConvertible, Equatable, Hashable {
61
55
public typealias Name = NSNotification . Name
62
56
63
57
/// Compare two notifications for equality.
64
- ///
65
- /// - note: Notifications that contain non NSObject values in userInfo will never compare as equal. This is because the type information is not preserved in the `userInfo` dictionary.
66
58
public static func == ( lhs: Notification , rhs: Notification ) -> Bool {
67
59
if lhs. name. rawValue != rhs. name. rawValue {
68
60
return false
@@ -80,10 +72,8 @@ public struct Notification : ReferenceConvertible, Equatable, Hashable {
80
72
}
81
73
if let lhsUserInfo = lhs. userInfo {
82
74
if let rhsUserInfo = rhs. userInfo {
83
- if lhsUserInfo. count != rhsUserInfo. count {
84
- return false
85
- }
86
- return _NSUserInfoDictionary. compare ( lhsUserInfo, rhsUserInfo)
75
+ // user info must be compared in the object form since the userInfo in swift is not comparable
76
+ return lhs. _bridgeToObjectiveC ( ) == rhs. _bridgeToObjectiveC ( )
87
77
} else {
88
78
return false
89
79
}
@@ -116,11 +106,7 @@ extension Notification : _ObjectiveCBridgeable {
116
106
117
107
@_semantics ( " convertToObjectiveC " )
118
108
public func _bridgeToObjectiveC( ) -> NSNotification {
119
- if let info = userInfo {
120
- return __NSNotificationCreate ( name. rawValue as NSString , object. map { $0 as AnyObject } , _NSUserInfoDictionary. bridgeValue ( from: info) )
121
- }
122
-
123
- return NSNotification ( name: name, object: object, userInfo: nil )
109
+ return NSNotification ( name: name, object: object, userInfo: userInfo)
124
110
}
125
111
126
112
public static func _forceBridgeFromObjectiveC( _ x: NSNotification , result: inout Notification ? ) {
@@ -130,18 +116,7 @@ extension Notification : _ObjectiveCBridgeable {
130
116
}
131
117
132
118
public static func _conditionallyBridgeFromObjectiveC( _ x: NSNotification , result: inout Notification ? ) -> Bool {
133
- if let userInfo = __NSNotificationUserInfo ( x) {
134
- if let info : [ String : Any ] ? = _NSUserInfoDictionary. bridgeReference ( from: userInfo) {
135
- result = Notification ( name: x. name, object: x. object, userInfo: info)
136
- return true
137
- } else {
138
- result = nil
139
- return false // something terrible went wrong...
140
- }
141
- } else {
142
- result = Notification ( name: x. name, object: x. object, userInfo: nil )
143
- }
144
-
119
+ result = Notification ( name: x. name, object: x. object, userInfo: x. userInfo)
145
120
return true
146
121
}
147
122
0 commit comments