Xcode 10 - image literals no longer available
XcodeImageBetaXcode10Image LiteralsXcode 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.
Double click on this icon and it will open the media library right side of it.
Just choose your imagem and it work like before!
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:
And then choose image from library and drag&drop it to your code
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")
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.
- Remove another, keep only one image
- Comment your line code with literal image, then will appear literal image in code, like:
#imageLiteral(resourceName: "[email protected]"
- in
resourceName
value, remove@2x
and file extension, like.png
, keep only image name:#imageLiteral(resourceName: "image")
- 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
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")!
)