> In the past bare strings were acceptable for postNotificationName and 
> addObserverForName.  Yet now constructing a Notification and 
> Notification.Name seem to be a bit unwieldy.  Example:
> 
> let ConnectedNotification    = 
> Notification(name:Notification.Name(rawValue:"ConnectedNotification"))
> 
> That seems to be quite convoluted and I'm not sure why there isn't a 
> convenience init that allows for
> 
> let ConnectedNotification    = Notification(name:"ConnectedNotification")

You're running into trouble because you're not following the typical pattern 
for these types. The intended usage is:

* You assign the Notification.Name to a constant.

        class ChatConnection {
                static let didConnect = 
Notification.Name("ChatConnection.didConnect")
                …
        }

* You create Notification instances on demand, or preferably let the 
NotificationCenter make them for you.

        extension ChatConnection {
                func connectionCompleted() {
                        NotificationCenter.default().post(name: 
ChatConnection.didConnect, object: self)
                }
        }

Individual Notifications are meant to be one-off instances. If you follow this 
pattern, you'll find that the API design suddenly makes a lot more sense!

-- 
Brent Royal-Gordon
Architechies

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev

Reply via email to