Xcode 10 - image literals no longer available

XcodeImageBetaXcode10Image Literals

Xcode Problem Overview


> Xcode 10 Release Notes: "Code Completion for Swift image literals has been removed in Xcode 10"

So it's not a bug that I couldn't add images in the code (and run older projects). How do we then add images from now on in the code as just typing UIImageView(image: won't accept any png or jpeg.

let cellImage: UIImageView = {
    let image = UIImageView(image: ???))
    
    return image
}()

System: macOS Mojave
Xcode: Xcode 10 Beta 4

Xcode Solutions


Solution 1 - Xcode

In Xcode 10 and Swift 4.2, only the code completion function (or auto-complete) of the Xcode IDE has been discontinued for the old way. Here is the new way:

Just type image literal and it will complete with default icon.

enter image description here

 

Double click on this icon and it will open the media library right side of it.

enter image description here

 

Just choose your imagem and it work like before!

enter image description here

Solution 2 - Xcode

Found the Shortcut to open the Media Library Tab:

++M

At this point the official documentation does not cover some use cases at Xcode 10 beta so I'm not able to find a way to move the media part to the old position.

Solution 3 - Xcode

You can use Media Library in Xcode to add image literal to your code:

Show XCode media library

And then choose image from library and drag&drop it to your code

Choose image from library

It will create image literal

Solution 4 - Xcode

For the benefit of others with legacy code that utlizes Swift Image Literals, the code and syntax of the image literal function themselves are still valid and work perfectly fine in Xcode 10 with Swift 4.2.

There is no need to rollback or refactor existing code that utlizes these image literals, and they can still be used as follows:

imageView.image = #imageLiteral(resourceName: imageNameString)

Only the code completion function (or auto-complete) of the Xcode IDE has been discontinued.


UPDATE

The difference in image-literal representation in previous and current versions or Xcode/Swift:

image = #imageLiteral(resourceName: "flower.jpg")

image = #imageLiteral(resourceName:

As the extract illustrates, image literals were represented in-line with thumbnails of their actual image. However, now, only a generic icon is used in its place. Presumably, this goes towards reducing the burden on the IDE, by eliminating the overheads of handling and displaying the in-line thumbnails.

Solution 5 - Xcode

TIP: To fix three resolutions after add a literal image in code.

  1. Remove another, keep only one image
  2. Comment your line code with literal image, then will appear literal image in code, like: #imageLiteral(resourceName: "[email protected]"
  3. in resourceName value, remove @2x and file extension, like .png, keep only image name: #imageLiteral(resourceName: "image")
  4. Uncomment! Image will be showed in code.

Solution 6 - Xcode

In Xcode 11, the media library is now under View > Show Library (SHIFT+CMD+L).

Solution 7 - Xcode

If anyone using Xcode 11 then you can find all media using below option:

enter image description here

enter image description here

Solution 8 - Xcode

Using

UIImage(imageLiteralResourceName: 'imageName')

won't return an optional and works just fine

Solution 9 - Xcode

i'm using Xcode 10 beta 4 and i have the same problem ! Resolved using the old way to pass an image

PizzaModel(
            nome: "Marinara",
            ingredienti: "Pomodoro, Aglio",
            calorie: "729",
            img: UIImage(named: "marinara")!
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPawel CichonskiView Question on Stackoverflow
Solution 1 - XcodeDaniel Arantes LoverdeView Answer on Stackoverflow
Solution 2 - XcodeRon GahlotView Answer on Stackoverflow
Solution 3 - Xcodeandrey.krukovskiyView Answer on Stackoverflow
Solution 4 - XcodeiSofiaView Answer on Stackoverflow
Solution 5 - XcodeWesley GomesView Answer on Stackoverflow
Solution 6 - XcodesamwizeView Answer on Stackoverflow
Solution 7 - XcodeguruView Answer on Stackoverflow
Solution 8 - XcodeFrancis KenView Answer on Stackoverflow
Solution 9 - XcodeGianloView Answer on Stackoverflow