read

If you have used #imageLiteral you might understand how dumb an Xcode feature can be. It is most likely a half-baked idea. Won’t be surprise to see them deprecate it.

How do you use?

Xcode wants you to use the Library pane (CMD + SHIFT + L).

You can add an image from the library by double clicking an image. To change, you double click the small inline preview (which shows a smaller pane for some weird reason)..

Iif you want to edit the image name (the text form), Xcode does not allow you to do that!

The way to the image name

The way to see/edit the actual image name in the source editor is to 🥁🥁… comment out the line!

That’s the only workaround I know.

I feel dumb.

The benefits?

It might seem nice to show a preview of the image. However, as developers, we need to see the code, the truth, and the textual representation.

Working with string can introduce accidental typos, which will cause crash if the resource is not available. HOWEVER, #imageLiteral does NOT perform compile time checks anyway. It can still crash!

If you see the default thumbnail, it is either Xcode have not load the image, or that the resource is unavailable (possible crash)!

Thumbnail Preview

The sole benefit is the thumbnail, yet it is not good enough. The preview shows only 1 of the appearance mode – either light or dark mode.

If you have transparent image, well, you can’t see that.

Eventually you have to open Asset Catalog, and see the full details. Xcode will not point you to the Asset Catalog.

I feel dumb whenever I have to open Asset Catalog manually.

The 2 inits

Since forever, we have UIImage initialization with the image name.

public init?(named name: String)

It will return nil if the resource is unavailable.

On the other hand, image literal seems to be using the following initialization:

/// Creates an instance initialized with the given resource name.
///
/// Do not call this initializer directly. Instead, initialize a variable or
/// constant using an image literal.
required public convenience init(imageLiteralResourceName name: String)

Ah-hah! It will not return nil. It will crash if the resource is unavailable!

As for the func comments, 🤷‍♂️

Conclusion

Do NOT use #imageLiteral or UIImage(imageLiteralResourceName:).

Use good old-fashioned UIImage(named:).


Image

@samwize

¯\_(ツ)_/¯

Back to Home