[swift-evolution] [Pitch] Make NSNotification names a protocol like ErrorProtocol

Brent Royal-Gordon brent at architechies.com
Wed Jun 29 19:11:43 CDT 2016


> While converting to Swift3, my biggest hurdle was figuring out what to do with a custom NSNotification. In Objective-C, it’s a plain NSString, and the same in Swift2.
> 
> I assumed in Swift3 that it worked like ErrorProtocol and errors as enums, but as it turns out, you need to create a custom instance of Notification.Name(“MyNotificationName”). This does not seem very Swifty, and I think it would work better if there was a NotificationNameProtocol and you could create enums with String raw values.

The migrator constructs the Notification.Name instances at the call site, but I believe the *actual* intent is that you should assign them to a constant—either one in your own type:

	// Old
	let MyClassWillFooNotificationName = "MyClassWillFoo"
	// New:
	class MyClass: … {
		static let WillFoo = Notification.Name("MyClass.WillFoo")
	}

Or in an extension to `Notification.Name` itself.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list