Xcode 10-图像文字不再可用

Xcode 10发行说明: “ Xcode 10中删除了用于 Swift 图像文字的代码完成”

因此,我不能在代码中添加图像(并运行较早的项目)并不是一个 bug。然后我们如何从现在开始在代码中添加图像,因为只输入 UIImageView(image:将不接受任何 png 或 jpeg。

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


return image
}()

系统: macOS Mojave
Xcode: Xcode 10 Beta 4

65505 次浏览

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")!
)

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.

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

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.

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

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

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

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

enter image description here

enter image description here

Just use #imageLiteral( It will work

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.