Loading image from Assets to NSImage keep getting error, expecting NSImage.Name

According to this answer in Apple Developer Forums:

... seems like NSImage(named: String) has been replaced by NSImage(named: NSImage.Name) in Swift 4.

So as suggested in the answer you can create an extension of the struct NSImage.Name:

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

And use it in this way:

let logoIcon = NSImage(named: .logo)

To load from the assets catalog of the current bundle, use something like:

let image = Bundle(for: type(of: self)).image(forResource: NSImage.Name(“logo"))!


Francesco Deliro's answer is correct, and you can avoid creating an extension to NSImage.Name with:

let logoIcon = NSImage(named: NSImage.Name("logo"))

Tags:

Swift

Nsimage