109

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pawel Cichonski
  • 1,099
  • 2
  • 7
  • 3
  • `UIImage(named: "myImage")` as in ye olde times (?) – Alladinian Jul 19 '18 at 09:20
  • 2
    Noteworthy: Only **code completion for image literals has been dropped**. The **image literals themselves continue to work fine** on XCode 10 with Swift 4.2. – iSofia Aug 19 '18 at 18:35
  • @isofia no they don’t because you can’t make one. – matt Aug 31 '18 at 03:05
  • 2
    @matt: `imageView.image = #imageLiteral(resourceName: imageNameString` - Autocomplete does not assist and the image is not represented, but it's totally valid and works perfectly. – iSofia Aug 31 '18 at 04:23
  • @iSofia (Hi, thanks for replying) But you're typing a _string_. So there's no merit to this approach; you might as well use `UIImage(named:)`. – matt Aug 31 '18 at 04:29
  • 1
    @matt: True, although the OP title **image literals no longer available** is inaccurate. The first comment and the one-provided answer also suggest the same, prescribing a rollback to the `UIImage(named:)` method, as you have. **Bottom line, no code refactoring necessary in this respect when migrating to Swift 4.2.** – iSofia Aug 31 '18 at 04:43
  • 3
    this is unfortunate, the image literal completion was a nice feature – Eric Armstrong Sep 23 '18 at 03:13
  • 2
    Possible duplicate of [Image suggestions are not working Xcode 10](https://stackoverflow.com/questions/51190850/image-suggestions-are-not-working-xcode-10) – Jay Patel Sep 28 '18 at 15:55
  • Don't forget you can add your own code completion for literals using menu Editor -> Create Code Snippet... – Marcy Mar 09 '22 at 17:04

12 Answers12

139

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Daniel Arantes Loverde
  • 2,317
  • 2
  • 28
  • 41
  • 72
    It's impossible to find an image by name this way. And my designer gave me all the assets as white-on-transparent png, so all the thumbnails are rendered as pure white in a random order... which image to pick is mission impossible. -_- – Cœur Sep 27 '18 at 05:30
  • 2
    Hi Coeur, please, tell your design to put all image on grey, then you change your assets to template, and then you can tint it on Storyboard or code :) – Daniel Arantes Loverde Sep 27 '18 at 12:25
  • Here is normal, double click open the list – Daniel Arantes Loverde Mar 18 '19 at 15:45
  • @Cœur it's not impossible but it's hard, if you go through project, #imageLiteral is just graphic representation of tokenized string, which makes it searchable with find (#imageLiteral in your current folder) :) – Dominik Bucher Jul 08 '20 at 09:06
  • 1
    @DominikBucher it's been two years and now I'm using Xcode 12, so the flow is to use `⇧`+`⌘`+`L` or the **+** button on top right of Xcode. – Cœur Jul 11 '20 at 14:41
99

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ron Gahlot
  • 1,396
  • 8
  • 18
55

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Thanks! For others: in the first screenshot, the *LEFTMOST* icon is pressed { } – xaphod Sep 18 '18 at 21:36
  • 3
    interestingly, this created invalid image literals for me when using pdf resources, resulting in a runtime crash - in the GM build. – David Ganster Sep 21 '18 at 12:16
  • 16
    This creates one literal for each `@1x`, `@2x`, and `@3x` asset and thus does not compile – Zappel Sep 24 '18 at 08:41
  • 2
    Seriously how is this supposed to work? Is there no way to drag and drop one representation of all three resolutions? This seems like a huge oversight by Apple. – aBikis Oct 14 '18 at 22:49
42

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: "flower.jpg")

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.

iSofia
  • 1,412
  • 2
  • 19
  • 36
  • 2
    Do we know why Apple took this out? Are image literals going to be depreciated in the future? – Bryan Hill Sep 24 '18 at 19:20
  • @BryanHill No explanation given, that I know of. But I've updated my answer, above, with a best-guess. – iSofia Sep 26 '18 at 05:41
  • I have marked that when you will see generic icon inplace of image. then your application will crashed in that place. – Badal Shah Oct 16 '18 at 10:00
  • Somehow I suddenly got thumbnails displayed inline when I selected the line containing it (Xcode 10.0 (10A255)). – heyfrank Oct 18 '18 at 11:51
  • I was able to get the preview images to show up by double clicking on the mountain icon, selecting different image, and the hitting cmd+z. Then all of the previews in one file appeared. – B Roy Dawson Nov 06 '18 at 18:03
  • 3
    I think it's just a bug – Reid Ellis Dec 06 '18 at 17:51
  • @BryanHill Some developers complained to Apple about image literals in code completion. But the code completion can be added back using Xcode's Editor -> Create code snippet... – Marcy Nov 16 '22 at 14:40
26

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: "image@2x.png"
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.

Wesley Gomes
  • 394
  • 3
  • 3
10

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

samwize
  • 25,675
  • 15
  • 141
  • 186
10

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

enter image description here

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
guru
  • 2,727
  • 3
  • 27
  • 39
7

Using

UIImage(imageLiteralResourceName: 'imageName')

won't return an optional and works just fine

Masood
  • 1,545
  • 1
  • 19
  • 30
Francis Ken
  • 71
  • 1
  • 1
2

Just use #imageLiteral( It will work

Shaikot37
  • 49
  • 5
1

you can use this code in Xcode 14.1

"UIImageVievName".image = #imageLiteral(resourceName: "imageName")

and then you can click the photo icon, you can chose what you want to add the image.

1

Just type command+shift+L , you will get the option for image addition.

0

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")!
)
Gianlo
  • 35
  • 5