Xcode 10 - image literals no longer available

巧了我就是萌 提交于 2019-11-28 07:54:15
Ron Gahlot

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.

Daniel Arantes Loverde

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!

andrey.krukovskiy

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

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.

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.

  • using UIImage(imageLiteralResourceName: 'imageName') won't return an optional and works just fine

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")!
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!