26

The most used feature is missing in Xcode 13

Image Literal

#imageLiteral()

these commands do not seem to be working. A similar change has happened for Color Literals as well.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rajesh
  • 10,318
  • 16
  • 44
  • 64
  • 1
    Did you check this https://stackoverflow.com/q/69346531/1187415 ? – Martin R Oct 08 '21 at 17:40
  • @MartinR That's not consistently working for me – Rajesh Oct 08 '21 at 17:45
  • 5
    I'd give up if I were you. Image literals and color literals have not worked usefully in years. Xcode 13 is merely the latest iteration. If, as your question implies, you use them a lot, stop doing so. – matt Oct 08 '21 at 17:49
  • 3
    In Xcode 13, use #imageLiteral( without the closing parenthesis. That will put the literal in the code and is the best workaround thus far. Code completion no longer works for literals in Xcode 13. – Marcy Oct 08 '21 at 18:08
  • @Rajesh, add images to the asset catalog first. The image literal requires images to be in an asset catalog before it will open. – Marcy Oct 08 '21 at 18:19

6 Answers6

26

There is no longer image literal :(

You may try:

Who.What = UIImage(named: "catImage")
aheze
  • 24,434
  • 8
  • 68
  • 125
Josie Koay
  • 735
  • 7
  • 8
  • 2
    This is simpler than #imageLiteral( because the popup picker sometimes does not works – wilcus Jan 15 '22 at 22:58
  • 1
    Xcode 14.2 allows one to use an image literal simply as typing imageView.image = #imageLiteral() at which point the #imageLiteral() in the above changed to a clickable icon allowing you to select any image you have placed into the projects assets. – PrimeDime Jan 26 '23 at 12:41
17

I came up with a workaround

Create code snippets for color literal and image literal like below

Color

#colorLiteral()

colorLiteral

And add completion shortcut as you want, Example: colorLiteral. Do same for Image literal

Image

#imageLiteral()

imageLiteral

And add completion shortcut as you want. Call those completion shortcuts in your code to get color and image literals.

colorLiteral imageLiteral

Suresh Mopidevi
  • 919
  • 3
  • 9
  • 24
10

type Who.What = #imageLiteral( Currently it works like this.

xeneen
  • 101
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 07 '21 at 16:03
7

I used

UIImage(imageLiteralResourceName: "image_asset_name")

and it works perfectly fine on XCode 13.2.1

Shishir
  • 169
  • 2
  • 6
0

You need to set a variable at the top of your struct to hold the color and image literals before you use them in the view body.

let color = #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)
let image = #imageLiteral(resourceName: "cherry1")

creating a shortcut is handy but it doesn't solve the problem of Xcode not allowing literals to be used as parameters to views.

This is a nice feature in Xcode and you may find it cool for small projects, but the best way to handle images, colours and other resources is to look into a package like R.Swift or Swiftgen

https://github.com/SwiftGen/SwiftGen

https://github.com/mac-cain13/R.swift

I prefer swiftgen since R.swift is not currently working with M1 Macs.

Richard Torcato
  • 2,504
  • 25
  • 26
0

I can't make Xcode auto complete colorLiteral, but you can still use the inline color picker.

This is a workaround which works in Xcode 14.2.

Where you want the color picker, enter any color full colorLiteral

#colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)

Xcode will then display the color picker which you can use.enter image description here

Joshua Dance
  • 8,847
  • 4
  • 67
  • 72