[swift-users] What is up with names not being Strings any more in Swift 4?

Joe Groff jgroff at apple.com
Mon Jul 17 10:47:26 CDT 2017


> On Jul 17, 2017, at 8:26 AM, Jon Shier via swift-users <swift-users at swift.org> wrote:
> 
> 	Like Notification.Name, I believe those types are supposed to help you namespace your string constants when you access them. I’d recommend using a code gen tool to generate them from your image assets, like:
> 
> extension NSImage.Name {
> 	static let customImage = NSImage.Name(rawValue: “customImage”)
> }
> 
> Which can then be used like: NSImage(named: .customImage). That’s the ultimate use goal. It does make the old code awkward and a pain to migrate though.

Yeah, this is the intended use pattern for these namespaced constant. You don't need the `rawValue:` label, though:

extension NSImage.Name {
  static let myImage = Name("myImage")
}

-Joe


More information about the swift-users mailing list