Xcode 12.4: Image Literal returns error message

I have tried to use the Image Literal on Xcode 12.4, and entered the following code:

Code Block
diceImageView1.imageView?.image = imageLiteral(resourceName: "DiceSix")


but it returns an error message which says:
Code Block
Value of type 'UIImageView' has no member 'imageView'

This error message prevents me from completing the build.

Any suggestions on how to handle this error?
You didn't tell what diceImageView1 is, but I understand it is an UIImageView.
Right ?

If so, you should write:
Code Block
diceImageView1.image =

then type
Code Block
image lit

and auto completion will propose to choose "DiceSix"
To choose image, you can open library / Media, from the View Menu in Xcode.

May be you need ? optional chaining, depending on the definition of diceImageView1

This will explain more: https://stackoverflow.com/questions/51397347/xcode-10-image-literals-no-longer-available
Thank you for your suggestion; I will give it a try.

I am following a course on learning Swift (I'm new to it); and the answer the teacher of the course is here: Xcode view controller

Have there been changes made to Swift and/or Xcode since she posted her solution?

You can type effectively #imageLiteral

But the code was with diceImageView1.image:
Code Block
diceImageView1.image = #imageLiteral(resourceName: "DiceSix")


Don't forget to close the thread if that's OK now.
I just found that the solution is:

Code Block
diceImageView1?.image = Image Literal


because diceImageView1 is already a UIImageView

This is my first experience with optionals in Swift.

Thank you.

image literal seems to not be working any further in Xcode 13. Does anybody know what's going on?

Hi, I searched the Web and found the temporary fix for this .

diceImageView1.image = UIImage(named: "DiceSix") diceImageView2.image = UIImage(named: "DiceTwo")

You might need to play around with the brackets " ( " ( delete them and putting them back and see what happen. Xcode is being naughty sometimes. ). I reloaded my Project by recloned it again from Github and started the same procedures over. It seemed to work for me. However, for the following lessons where you will have to put these images in the arrays, I will need to try and perhaps search for solutions otherwise the line of code will be very long. If you have the solutions, I would appreciate if you can share.

Rick

Hi, I found this from Udemy Lesson Q&A from a user name LEE. This solution works for those of us who just starting to learn Swift. Thank you to LEE and would like his permission to share his answer below. Thanks again Lee.

Rick

Hi @Oleksandr,

I had the same issue with Xcode 13 also as it wasn't coming up in the dictionary suggestions when typing 'image literal'.

I've found that if you type the following then it will work and present you with the image icon and let you select the image as Angela has shown:

diceImageView1.image = #imageLiteral(

As soon as you type the trailing '(' then it will change to an image icon and let you select the DiceSix image.

Hope that helps!

Cheers,

Lee

If you are following Angela Yu's Course, Here is how to fix the problem.

Type

#imageLiteral(

and the Xcode will auto-complete.

Note

  1. Make sure the "L" is capital in Literal.
16

Hi. Yes, I can agree. All you need is to type

diceImageView1.image = #imageLiteral(

then you will see a desirable symbol.

Hi,

I typed diceImageView1.image = #imageliteral and it doesn't work for me

Instead of typing imageLiteral and let Xcode autocomplete the code, you can copy and paste this:

#imageLiteral(

P.S With the open bracket

Once you paste it after the "=" it will work

e.g

diceImageView1.image = #imageLiteral(

Thanks CPiyabootr for the posting the answer. I am using XCode 13.1, imageLiteral by itself no longer works.

#imageLiteral( has to be typed out INSTEAD of typing imageLiteral and selecting the first choice as expressed in the Udemy course.

To be on the safer side make sure to type the code complete like this

diceImageView1.image = #imageLiteral()

yes it works when you need one image, diceImageView1.image = #imageLiteral( but when you need array of images this is not working , there is a problem

If you're following the Udemy Course for the Dicee-ios13 project use #imageLiteral( then press enter. This will allow you to select the image "DiceSix". This is running with latest version of Xcode (13.2.1).

Use the following code:-

diceImageView.image = #imageLiteral(

it will automatically set you an arrays of pictures you have set for your project!

Thank you!

Syed Ajaz Mumtaz

If you can't see the image literal shortcut pop up, you can manually write the code for it and Xcode will replace that code with the required image once the code is complete. For example: diceImageView1.image = #imageLiteral(resourceName: "DiceOne") If you are writing the array of image literals, you can write the code for one image literal manually and then copy-paste the code and change the picture manually. Example: diceArray = [#imageLiteral(resourceName: "DiceOne") , #imageLiteral(resourceName: "DiceTwo"), ..... ]

Hello, I had same problem. But I solved it. Here's my solution. You have to write diceImageView.view = #imageLiteral'( Then the Xcode show you to images you can choose

not diceImageView1.image = #imageLiteral( nor diceImageView1.view = #imageLiteral( doesn't work

I had the same issues, after reading through the comments and solutions here (and tweaking it slightly) this worked for me:


diceImageView1.image = #imageLiteral(

the open bracket above was essential to get it to work.

This is interesting, I wonder if this is a bug?

I ran into this earlier on Xcode Version 13.4.1 (13F100). I was going to run instead the #imageLiteral(resourceName: but a funny thing happened. After typing #imageLiteral(resourceName: I see the autocomplete and use it.

I get the following code.  #init(imageLiteralResourceName: <#T##String#>)

When I went to remove the #init( as I thought it was strange and wanted to see what would happen I ended up putting a ( between Literal and Resource, basically entering the following command. #imageLiteral(

Subsequently, you can enter #imageLiteral( and get the box to appear but the autocomplete won't work. However, as soon as you enter the ( it pops the box in place of the code.

Hope this helps someone.

Suddenly the #imageLiteral( pic selection box appears. So it seems the function still exists but is bugged and not loading as it should ( assuming it wasn't removed on purpose for some reason above my beginner pay grade... cause I am making the Dicee App....."

Ola pessoal, para aqueles que por alguma razão estão tentando usar o image literal e ele não busca a opção de selecionar, eu encontrei aqui mesmo no foram a solução. Quando estiver digitando inicie com # e termine com ( isso mesmo...vai funcionar. fica assim #imageLiteral( Observação estou usando o Xcode 13.4.1

In the newest Xcode14.0, you can still use #imageLiteral( and press tab or enter to auto generate the select button, and then double click to choose the image you need.

e.g

diceImageView1.image = #imageLiteral(;
# after press tab/enter and choose the image
diceImageView1.image = imageLiteral(resourceName: "DiceSix");

P.S Don't forget the left bracket and the whitespace around "=" (or double click won't work)

I got quick solution for any Xcode version
step:1 enter all the code in comment //diceImageView1.image = imageLiteral(resourceName: "DiceSix");

step:2 choose your image based on your choice by entering image name in between semicolon

step3: uncomment your line and boom its gonna work for sure

mohammed iftekhar

I am new at coding and new to SWIFT, and I have started the Udemy  course by Dr.Angela Yu. The course is grate but it is in Xcode 13, and I am using Xcode 14, and it is different. After a few chapters I found my self in trouble with Image Literals. didn't see the small icon in the displayed code, I even had to write the "name_of_the_file" . After looking and looking and trying, the only easy way to do it was this.

code:

# imageLiteral() ---->write the pound key# and write imageLiteral and then the brackets()
and then double click in the little image and the display to select an image on the assets folder will appear. !!!! for some reason the displayed suggestions when starting to type imageliteral, does not show this option!!!!!

so:

1)Type : #imageLiteral

  1. then type : ()

A little icon will appear, double click it and select the image from the assets folder.

Xcode 12.4: Image Literal returns error message
 
 
Q