I am seeing imageNamed deprecated (or removed) from available options When I do the following:
var statusImage:NSImage? = nil
self.statusImage = NSImage .ImageNamed....
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With