Xcode 10 - 图像文字不再可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51397347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 10 - image literals no longer available
提问by Pawel Cichonski
Xcode 10 Release Notes: "Code Completion for Swift image literals has been removed in Xcode 10"
Xcode 10 发行说明:“Xcode 10 中删除了 Swift 图像文字的代码完成”
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.
因此,我无法在代码中添加图像(并运行旧项目)并不是一个错误。从现在开始,我们如何在代码中添加图像,因为仅键入UIImageView(image:
将不接受任何 png 或 jpeg。
let cellImage: UIImageView = {
let image = UIImageView(image: ???))
return image
}()
System: macOS Mojave
Xcode: Xcode 10 Beta 4
系统:macOS Mojave
Xcode:Xcode 10 Beta 4
回答by 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:
在Xcode 10 和 Swift 4.2 中,只有 Xcode IDE 的代码完成功能(或自动完成)已停止使用旧方式。这是新方法:
Just type image literal
and it will complete with default icon.
只需键入image literal
,它将以默认图标完成。
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!
只需选择您的图像,它就可以像以前一样工作!
回答by Ron Gahlot
Found the Shortcut to open the Media Library Tab:
找到打开媒体库选项卡的快捷方式:
?+?+M
?+ ?+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.
在这一点上,官方文档没有涵盖 Xcode 10 beta 的一些用例,所以我无法找到将媒体部分移动到旧位置的方法。
回答by andrey.krukovskiy
回答by iSofia
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.
为了其他使用Swift Image Literals 的遗留代码的人的利益,图像文字函数本身的代码和语法仍然有效,并且在 Xcode 10 和 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.
只有 Xcode IDE 的代码完成功能(或自动完成)已停止使用。
UPDATE
更新
The difference in image-literal representation in previous and current versions or Xcode/Swift:
先前和当前版本或 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.
正如摘录所示,图像文字与其实际图像的缩略图一起表示。但是,现在,仅使用通用图标来代替它。据推测,通过消除处理和显示内嵌缩略图的开销,这有助于减轻 IDE 的负担。
回答by Wesley Gomes
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.
提示:在代码中添加文字图像后修复三个分辨率。
1. 删除另一个,只保留一个图像
2. 用文字图像注释您的行代码,然后将在代码中出现文字图像,例如:#imageLiteral(resourceName: "[email protected]"
3. 在resourceName
值,删除@2x
和文件扩展名中,例如.png
,仅保留图像名称:#imageLiteral(resourceName: "image")
4. 取消注释!图像将显示在代码中。
回答by samwize
In Xcode 11, the media library is now under View > Show Library (SHIFT+CMD+L).
在 Xcode 11 中,媒体库现在位于 View > Show Library (SHIFT+CMD+L) 下。
回答by guru
回答by Francis Ken
- using UIImage(imageLiteralResourceName: 'imageName') won't return an optional and works just fine
- 使用 UIImage(imageLiteralResourceName: 'imageName') 不会返回一个可选的并且工作得很好
回答by Gianlo
i'm using Xcode 10 beta 4 and i have the same problem ! Resolved using the old way to pass an image
我正在使用 Xcode 10 beta 4,我遇到了同样的问题!解决使用旧方式传递图像
PizzaModel(
nome: "Marinara",
ingredienti: "Pomodoro, Aglio",
calorie: "729",
img: UIImage(named: "marinara")!
)