- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathNotification.swift
120 lines (99 loc) · 3.87 KB
/
Notification.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/**
`Notification` encapsulates information broadcast to observers via a `NotificationCenter`.
*/
publicstructNotification:ReferenceConvertible,Equatable,Hashable{
publictypealiasReferenceType=NSNotification
/// A tag identifying the notification.
publicvarname:Name
/// An object that the poster wishes to send to observers.
///
/// Typically this is the object that posted the notification.
publicvarobject:Any?
/// Storage for values or objects related to this notification.
publicvaruserInfo:[AnyHashable:Any]?
/// Initialize a new `Notification`.
///
/// The default value for `userInfo` is nil.
publicinit(name:Name, object:Any?=nil, userInfo:[AnyHashable:Any]?=nil){
self.name = name
self.object = object
self.userInfo = userInfo
}
publicvarhashValue:Int{
return name.rawValue.hash
}
publicvardescription:String{
vardescription="name = \(name.rawValue)"
iflet obj = object { description +=", object = \(obj)"}
iflet info = userInfo { description +=", userInfo = \(info)"}
return description
}
publicvardebugDescription:String{
return description
}
// FIXME: Handle directly via API Notes
publictypealiasName=NSNotification.Name
publicstaticfunc==(lhs:Notification, rhs:Notification)->Bool{
if lhs.name.rawValue != rhs.name.rawValue {
returnfalse
}
iflet lhsObj = lhs.object {
iflet rhsObj = rhs.object {
if _SwiftValue.store(lhsObj)!== _SwiftValue.store(rhsObj){
returnfalse
}
}else{
returnfalse
}
}elseif rhs.object !=nil{
returnfalse
}
returntrue
}
}
extensionNotification:CustomReflectable{
publicvarcustomMirror:Mirror{
varchildren:[(label:String?, value:Any)]=[(label:"name",self.name.rawValue)]
iflet object =self.object {
children.append((label:"object", object))
}
iflet info =self.userInfo {
children.append((label:"userInfo", info))
}
returnMirror(self, children: children, displayStyle:.class)
}
}
extensionNotification:_ObjectiveCBridgeable{
publicstaticfunc _getObjectiveCType()->Any.Type{
returnNSNotification.self
}
@_semantics("convertToObjectiveC")
publicfunc _bridgeToObjectiveC()->NSNotification{
returnNSNotification(name: name, object: object, userInfo: userInfo)
}
publicstaticfunc _forceBridgeFromObjectiveC(_ x:NSNotification, result:inoutNotification?){
if !_conditionallyBridgeFromObjectiveC(x, result:&result){
fatalError("Unable to bridge type")
}
}
publicstaticfunc _conditionallyBridgeFromObjectiveC(_ x:NSNotification, result:inoutNotification?)->Bool{
result =Notification(name: x.name, object: x.object, userInfo: x.userInfo)
returntrue
}
publicstaticfunc _unconditionallyBridgeFromObjectiveC(_ source:NSNotification?)->Notification{
varresult:Notification?=nil
_forceBridgeFromObjectiveC(source!, result:&result)
return result!
}
}