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).

Xcode 12.4: Image Literal returns error message
 
 
Q