skip to Main Content

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.

6

Answers


  1. There is no longer image literal 🙁

    You may try:

    Who.What = UIImage(named: "catImage")
    
    Login or Signup to reply.
  2. type Who.What = #imageLiteral(
    Currently it works like this.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. I used

    UIImage(imageLiteralResourceName: "image_asset_name")

    and it works perfectly fine on XCode 13.2.1

    Login or Signup to reply.
  5. 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.

    Login or Signup to reply.
  6. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search