Commit cab632f5 authored by Aral Balkan's avatar Aral Balkan
Browse files

Update API for Swift 3 (userInfo in signature)

Also, update demo to add example of event posting.
parent 113f6680
......@@ -81,7 +81,7 @@ public func handle(_ notificationName:String, with block:NotificationBlock) -> N
}
public func handle(_ notificationName:String, from object:AnyObject?, with block:NotificationBlock) -> NotificationHandler
public func handle(_ notificationName:String, from object:Any?, with block:NotificationBlock) -> NotificationHandler
{
let observer = NotificationHandler(handler: block)
📡.addObserver(observer, selector: #selector(observer.handlerProxy(_:)), name: NSNotification.Name(rawValue: notificationName), object: object)
......@@ -94,14 +94,14 @@ public func handle(_ notificationName:String, from object:AnyObject?, with block
//
public func post(_ notificationName:String, from object:AnyObject?, with userInfo:[NSObject : AnyObject]?)
public func post(_ notificationName:String, from object:Any?, with userInfo:[AnyHashable:Any]?)
{
// println("About to post: \(notificationName) notification from \(object) with userInfo: \(userInfo)")
📡.post(name: NSNotification.Name(rawValue: notificationName), object: object, userInfo: userInfo)
}
public func post(_ notificationName:String, from object:AnyObject?)
public func post(_ notificationName:String, from object:Any?)
{
📡.post(name: NSNotification.Name(rawValue: notificationName), object: object, userInfo: /* userInfo as */ nil)
}
......@@ -124,13 +124,13 @@ public func post(_ notificationName:String)
// MARK: - Handling notifications
//
public func handle(_ notificationName:String, with selector:Selector, on observer:AnyObject)
public func handle(_ notificationName:String, with selector:Selector, on observer:Any)
{
📡.addObserver(observer, selector: selector, name: NSNotification.Name(rawValue: notificationName), object: nil)
}
public func handle(_ notificationName:String, from object:AnyObject?, with selector:Selector, on observer:AnyObject)
public func handle(_ notificationName:String, from object:Any?, with selector:Selector, on observer:Any)
{
📡.addObserver(observer, selector: selector, name: NSNotification.Name(rawValue: notificationName), object: object)
}
......@@ -140,13 +140,13 @@ public func handle(_ notificationName:String, from object:AnyObject?, with selec
// MARK: - Removing notification handlers.
//
public func remove(observer:AnyObject, forNotificationNamed name:String?)
public func remove(observer:Any, forNotificationNamed name:String?)
{
📡.removeObserver(observer, name: name.map { NSNotification.Name(rawValue: $0) }, object: nil)
}
public func remove(observer:AnyObject, forNotificationNamed name:String?, from object:AnyObject?)
public func remove(observer:Any, forNotificationNamed name:String?, from object:Any?)
{
📡.removeObserver(observer, name: name.map { NSNotification.Name(rawValue: $0) }, object: object)
}
......@@ -7,15 +7,37 @@
//
import UIKit
import Handle
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
/// To demonstrate notification handling.
var demoNotificationHandler: NotificationHandler?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
{
// Add handler
demoNotificationHandler = demoNotificationHandler ?? handle(DemoNotification.named(.orientationDidChange))
{
/* with */ notification in
guard
let userInfo = notification.userInfo,
let orientationName = userInfo["orientationName"]
else
{
print("Warning: Could not get notification name.")
return
}
print("Got orientation change notification in App Delegate.\nOrientation is now: \(orientationName)")
}
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
......
......@@ -9,6 +9,17 @@
import UIKit
import Handle
enum DemoNotification:String
{
case orientationDidChange
static func named(_ notificationName: DemoNotification) -> String
{
return notificationName.rawValue
}
}
class ViewController: UIViewController
{
@IBOutlet weak var orientationLabel: UILabel!
......@@ -30,6 +41,9 @@ class ViewController: UIViewController
guard let orientation = notification.userInfo?[UIApplicationStatusBarOrientationUserInfoKey] as? Int else { return }
let orientationName = self.orientationNames[orientation]
// Post a notification
post(DemoNotification.named(.orientationDidChange), from: self, with: ["orientationName": orientationName])
self.orientationLabel.text = "\(orientationName) orientation."
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment