0001    //
0002    //  Created by Jesse Squires
0003    //  http://www.jessesquires.com
0004    //
0005    //
0006    //  Documentation
0007    //  http://www.jessesquires.com/JSQNotificationObserverKit
0008    //
0009    //
0010    //  GitHub
0011    //  https://github.com/jessesquires/JSQNotificationObserverKit
0012    //
0013    //
0014    //  License
0015    //  Copyright © 2015-present Jesse Squires
0016    //  Released under an MIT license: http://opensource.org/licenses/MIT
0017    //
0018    
0019    import Foundation
0020    
0021    /// Describes a notification's userInfo dictionary
0022    public typealias UserInfo
NotificationObserver.swift:31
public func ==(lhs: UserInfo, rhs: UserInfo) -> Bool {
NotificationObserver.swift:31
public func ==(lhs: UserInfo, rhs: UserInfo) -> Bool {
NotificationObserver.swift:220
private func unboxUserInfo<T>(userInfo: UserInfo?) -> T? {
= [NSObject : AnyObject] 0023 0024 0025 /** 0026 - parameter lhs: A UserInfo instance. 0027 - parameter rhs: A UserInfo instance. 0028 0029 - returns: True if `lhs` is equal to `rhs`, false otherwise. 0030 */ 0031 public func ==(lhs: UserInfo, rhs: UserInfo) -> Bool { 0032 guard lhs.count == rhs.count else { 0033 return false 0034 } 0035 0036 for (key, value) in lhs { 0037 guard let rhsValue = rhs[key] else { 0038 return false 0039 } 0040 0041 if !rhsValue.isEqual(value) { 0042 return false 0043 } 0044 } 0045 return true 0046 } 0047 0048 0049 /** 0050 A typed notification that contains a name and optional sender. 0051 0052 - note: The `Value` type parameter acts as a phantom type, restricting the notification to posting only values of this type. 0053 */ 0054 public struct Notification
NotificationObserver.swift:99
    public func withSender(sender: Sender?) -> Notification {
NotificationObserver.swift:100
        return Notification(name: name, sender: sender)
NotificationObserver.swift:155
        notification: Notification<V, S>,
NotificationObserver.swift:181
        notification: Notification<V, S>,
<Value
NotificationObserver.swift:86
    public func post(value: Value, center: NSNotificationCenter = .defaultCenter()) {
, Sender
NotificationObserver.swift:62
    public let sender: Sender?
NotificationObserver.swift:74
    public init(name: String, sender: Sender? = nil) {
NotificationObserver.swift:99
    public func withSender(sender: Sender?) -> Notification {
: AnyObject> { 0055 0056 // MARK: Properties 0057 0058 /// The name of the notification. 0059 public let name
NotificationObserver.swift:75
        self.name = name
NotificationObserver.swift:87
        center.postNotificationName(name, object: sender, userInfo: userInfo(value))
NotificationObserver.swift:100
        return Notification(name: name, sender: sender)
NotificationObserver.swift:186
            observerProxy = center.addObserverForName(notification.name, object: notification.sender, queue: queue, usingBlock: handler)
: String 0060 0061 /// The object that posted the notification. 0062 public let sender
NotificationObserver.swift:76
        self.sender = sender
NotificationObserver.swift:87
        center.postNotificationName(name, object: sender, userInfo: userInfo(value))
NotificationObserver.swift:186
            observerProxy = center.addObserverForName(notification.name, object: notification.sender, queue: queue, usingBlock: handler)
: Sender? 0063 0064 // MARK: Initialization 0065 0066 /** 0067 Constructs a new notification instance having the specified name and sender. 0068 0069 - parameter name: The name of the notification. 0070 - parameter sender: The object sending the notification. The default is `nil`. 0071 0072 - returns: A new `Notification` instance. 0073 */ 0074 public init
NotificationObserver.swift:100
        return Notification(name: name, sender: sender)
(name: String, sender: Sender? = nil) { 0075 self.name = name 0076 self.sender = sender 0077 } 0078 0079 /** 0080 Posts the notification with the given value to the specified center. 0081 0082 - parameter value: The data to be sent with the notification. 0083 - parameter center: The notification center from which the notification should be dispatched. 0084 The default is `NSNotificationCenter.defaultCenter()`. 0085 */ 0086 public func post(value: Value, center: NSNotificationCenter = .defaultCenter()) { 0087 center.postNotificationName(name, object: sender, userInfo: userInfo(value)) 0088 } 0089 0090 /** 0091 Returns a new notification with the receiver's name and the specified sender. 0092 0093 - warning: Note that this function returns a new `Notification` instance. 0094 0095 - parameter sender: The instance posting this notification. 0096 0097 - returns: A new `Notification` instance. 0098 */ 0099 public func withSender(sender: Sender?) -> Notification { 0100 return Notification(name: name, sender: sender) 0101 } 0102 } 0103 0104 0105 /** 0106 An instance of `NotificationObserver` is responsible for observing notifications. 0107 0108 - note: When an observer is initialized, it will immediately begin listening for its specified notification 0109 by registering with the specified notification center. 0110 */ 0111 public final class NotificationObserver <V
NotificationObserver.swift:121
    public typealias ValueSenderHandler = (value: V, sender: S?) -> Void
NotificationObserver.swift:155
        notification: Notification<V, S>,
NotificationObserver.swift:160
                if let value: V = unboxUserInfo(notification.userInfo) {
NotificationObserver.swift:181
        notification: Notification<V, S>,
, S
NotificationObserver.swift:121
    public typealias ValueSenderHandler = (value: V, sender: S?) -> Void
NotificationObserver.swift:155
        notification: Notification<V, S>,
NotificationObserver.swift:161
                    handler(value: value, sender: notification.object as? S)
NotificationObserver.swift:181
        notification: Notification<V, S>,
: AnyObject> { 0112 0113 // MARK: Typealiases 0114 0115 /** 0116 The closure to be called when a `Notification` is received. 0117 0118 - parameter value: The data sent with the notification. 0119 - parameter sender: The object that sent the notification, or `nil` if the notification is not associated with a specific sender. 0120 */ 0121 public typealias ValueSenderHandler
NotificationObserver.swift:158
        handler: ValueSenderHandler) {
= (value: V, sender: S?) -> Void 0122 0123 /** 0124 The closure to be called when an `NSNotification` is received. 0125 0126 - parameter notification: The notification received. 0127 */ 0128 public typealias NotificationHandler
NotificationObserver.swift:184
        handler: NotificationHandler) {
= (notification: NSNotification) -> Void 0129 0130 0131 // MARK: Properties 0132 0133 private let observerProxy
NotificationObserver.swift:186
            observerProxy = center.addObserverForName(notification.name, object: notification.sender, queue: queue, usingBlock: handler)
NotificationObserver.swift:191
        center.removeObserver(observerProxy)
: NSObjectProtocol 0134 0135 private let center
NotificationObserver.swift:185
            self.center = center
NotificationObserver.swift:191
        center.removeObserver(observerProxy)
: NSNotificationCenter 0136 0137 0138 // MARK: Initialization 0139 0140 /** 0141 Constructs a new `NotificationObserver` instance and immediately registers to begin observing the specified `notification`. 0142 0143 - warning: To unregister this observer and end listening for notifications, dealloc the object by setting it to `nil`. 0144 0145 - parameter notification: The notification for which to register the observer. 0146 - parameter queue: The operation queue to which `handler` should be added. 0147 If `nil` (the default), the block is run synchronously on the posting thread. 0148 - parameter center: The notification center from which the notification should be dispatched. 0149 The default is `NSNotificationCenter.defaultCenter()`. 0150 - parameter handler: The closure to execute when the notification is received. 0151 0152 - returns: A new `NotificationObserver` instance. 0153 */ 0154 public convenience init( 0155 notification: Notification<V, S>, 0156 queue: NSOperationQueue? = nil, 0157 center: NSNotificationCenter = .defaultCenter(), 0158 handler: ValueSenderHandler) { 0159 self.init(notification: notification, queue: queue, center: center, handler: { (notification: NSNotification) in 0160 if let value: V = unboxUserInfo(notification.userInfo) { 0161 handler(value: value, sender: notification.object as? S) 0162 } 0163 }) 0164 } 0165 0166 /** 0167 Constructs a new `NotificationObserver` instance and immediately registers to begin observing the specified `notification`. 0168 0169 - warning: To unregister this observer and end listening for notifications, dealloc the object by setting it to `nil`. 0170 0171 - parameter notification: The notification for which to register the observer. 0172 - parameter queue: The operation queue to which `handler` should be added. 0173 If `nil` (the default), the block is run synchronously on the posting thread. 0174 - parameter center: The notification center from which the notification should be dispatched. 0175 The default is `NSNotificationCenter.defaultCenter()`. 0176 - parameter handler: The closure to execute when the notification is received. 0177 0178 - returns: A new `NotificationObserver` instance. 0179 */ 0180 public init
NotificationObserver.swift:159
            self.init(notification: notification, queue: queue, center: center, handler: { (notification: NSNotification) in
( 0181 notification: Notification<V, S>, 0182 queue: NSOperationQueue? = nil, 0183 center: NSNotificationCenter = .defaultCenter(), 0184 handler: NotificationHandler) { 0185 self.center = center 0186 observerProxy = center.addObserverForName(notification.name, object: notification.sender, queue: queue, usingBlock: handler) 0187 } 0188 0189 /// :nodoc: 0190 deinit { 0191 center.removeObserver(observerProxy) 0192 } 0193 } 0194 0195 0196 // MARK: Private 0197 0198 0199 // Key for user info dictionary 0200 private let UserInfoValueKey
NotificationObserver.swift:215
    return [UserInfoValueKey : Box(value)]
NotificationObserver.swift:221
    if let box = userInfo?[UserInfoValueKey] as? Box<T> {
= "UserInfoValueKey" 0201 0202 0203 // This class allows the "boxing up" instances that are not reference types 0204 private final class Box
NotificationObserver.swift:214
private func userInfo<T>(value: T) -> [String : Box<T>] {
NotificationObserver.swift:215
    return [UserInfoValueKey : Box(value)]
NotificationObserver.swift:221
    if let box = userInfo?[UserInfoValueKey] as? Box<T> {
<T
NotificationObserver.swift:205
    let value: T
NotificationObserver.swift:207
    init(_ value: T) {
> { 0205 let value
NotificationObserver.swift:208
        self.value = value
NotificationObserver.swift:222
        return box.value
: T 0206 0207 init
NotificationObserver.swift:215
    return [UserInfoValueKey : Box(value)]
(_ value: T) { 0208 self.value = value 0209 } 0210 } 0211 0212 0213 // Create user info dictionary 0214 private func userInfo
NotificationObserver.swift:87
        center.postNotificationName(name, object: sender, userInfo: userInfo(value))
<T>(value: T) -> [String : Box<T>] { 0215 return [UserInfoValueKey : Box(value)] 0216 } 0217 0218 0219 // Unbox user info dictionary 0220 private func unboxUserInfo
NotificationObserver.swift:160
                if let value: V = unboxUserInfo(notification.userInfo) {
<T>(userInfo: UserInfo?) -> T? { 0221 if let box = userInfo?[UserInfoValueKey] as? Box<T> { 0222 return box.value 0223 } 0224 return nil 0225 } 0226