Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does NSImage.Name exist?

Tags:

macos

swift

cocoa

I don't understand why for creating UIImage I just pass String in constructor but for creating NSImage I have to pass NSImage.Name in constructor. What idea is laying behind this solution?

Code:

iOS

let image = UIImage(named: "name")

Mac OS

let image = NSImage(named: NSImage.Name("name"))

Thanks.

like image 861
Bohdan Savych Avatar asked Jul 10 '18 16:07

Bohdan Savych


1 Answers

The expectation is that you have a centralized spot where you extend NSImage.Name with static constants that define all your image names. Then, all your code references these single members, rather than repeating magic strings.

extension NSImage.Name {
    static let square = NSImage.Name("square")
    static let triangle = NSImage.Name("triangle")
    static let circle = NSImage.Name("circle")
}

//...

let image = NSImage(named: .square)
like image 92
Alexander Avatar answered Nov 15 '22 04:11

Alexander