Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Can not use NSImage .imageNamed

I am seeing imageNamed deprecated (or removed) from available options When I do the following:

       var statusImage:NSImage? = nil

       self.statusImage = NSImage .ImageNamed....

enter image description here

I have tried the Swift document provided by Apple and other placed.This seemed very trivial but could not find the solution for imageNamed. Am I missing something?

like image 566
Obj-Swift Avatar asked Jun 18 '14 21:06

Obj-Swift


1 Answers

Use init(named: String!) instead: call it like NSImage(named: "foo").

The compiler automatically remaps ObjC class methods that are named as convenience constructors to work as Swift initializers. If a class method follows the naming convention of a convenience constructor (e.g. +[SomeThing thingWithFoo: bar:]), Swift remaps it to an initializer (e.g. call SomeThing(foo: aFoo, bar: aBar)). This also goes for a few methods that Apple identified as working like a convenience constructor (as in the case of imageNamed:).

In most cases, if you finish typing the class-method-style call to a convenience constructor, the compiler will give you an error that tells you how it's been remapped:

error: 'imageNamed' is unavailable: use object construction 'NSImage(named:)'

More generally, you can look at the autogenerated module "header" for an API symbol in Xcode by cmd-clicking that symbol (e.g. NSImage) in the editor, or look in Xcode's documentation viewer or the online reference docs for that API, to find the Swift syntax for using it.

like image 59
rickster Avatar answered Oct 02 '22 16:10

rickster