Notification.post is defined as:
public func post(name aName: NSNotification.Name, object anObject: AnyObject?)
In Objective-C, the notification name is a simple NSString. In Swift, it is defined as NSNotification.Name.
NSNotification.Name is defined as:
public struct Name : RawRepresentable, Equatable, Hashable, Comparable { public init(_ rawValue: String) public init(rawValue: String) }
This is a bit strange, as I expect it to be Enum, and not some kind of user structure that would seem to be of no more use.
In Notification for NSNotification.Name: typealias.Name:
public typealias Name = NSNotification.Name
The confusing part is that both notification and NSNotification exist in Swift
So, to define your own notification, do the following:
public class MyClass { static let myNotification = Notification.Name("myNotification") }
Then call it:
NotificationCenter.default().post(name: MyClass.myNotification, object: self)
hexdreamer Jun 18 '16 at 17:29 2016-06-18 17:29
source share