Xcode 8 图像补全

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39610994/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:18:22  来源:igfitidea点击:

Xcode 8 Image Completion

iosswiftxcodemacosswift3

提问by CZ54

In Xcode 8, you have images autocompleted while typing.

在 Xcode 8 中,您可以在键入时自动完成图像。

Question is : Why?

问题是:为什么?

I tried to init UIImage with the suggested result, but it doesn't work.

我试图用建议的结果初始化 UIImage,但它不起作用。

Inside UIImageOutside UIImage

内部 UIImage外部 UIImage

Does anyone know how to use it ?

有谁知道如何使用它?

回答by Rashwan L

Xcode 8 will automatically recognize any images you've got in an Asset Catalog and offer them up as a suggestion inside of a UIImage initializer.

Xcode 8 将自动识别您在资产目录中获得的任何图像,并将它们作为建议提供给 UIImage 初始值设定项。

So basically what you need to do is just the following (as you have done in your question, but there must be something else that′s disturbing it):

所以基本上你需要做的就是以下内容(正如你在你的问题中所做的那样,但肯定还有其他事情会打扰它):

let i = voiture // image name

And then just use iwhen you want to set an image.

然后i在你想设置图像时使用。

Under the hood it's creating code that looks like this: #imageLiteral(resourceName: "voiture.png"). But inline in the source editor, you'll just see the file name of the image. The #imageLiteralsyntax is only recognised on Swift 3or later.

引擎盖下它的创建代码,看起来像这样:#imageLiteral(resourceName: "voiture.png")。但是在源代码编辑器中内联,您只会看到图像的文件名。该#imageLiteral语法仅在Swift 3或更高版本上被识别。

Hereis a demonstration videolink where I do this and hereis a link to a sample projectthat I created.

这里是一个演示视频链接,我做到这一点,这里是一个链接示例项目,我创建的。

Notethat you need to click on the instellisense suggestion so that you see a thumbnail of the image in the code and then the image name.

请注意,您需要单击 instellisense 建议,以便您在代码中看到图像的缩略图,然后是图像名称。

Update 1

更新 1

This functionality remains in Xcode Version 9.0 beta 6 (9M214v)enter image description here

此功能保留在Xcode 版本 9.0 beta 6 (9M214v) 中在此处输入图片说明

Update 2

更新 2

Xcode 11.2.1, this is not appearing anymore.

Xcode 11.2.1,这不再出现。

回答by Chris Hanson

An image literal already is a UIImage, you don't need to pass it to a UIImage to initialize one.

一个图像文字已经是一个 UIImage,你不需要将它传递给一个 UIImage 来初始化一个。

回答by Dave Lyon

I'm unable to find an "official" source for it, but from my personal testing it appears that while the autocomplete works in both Swift 2.3 and Swift 3.0 projects, image literals will only work correctly in a Swift 3 project.

我找不到它的“官方”来源,但从我的个人测试来看,虽然自动完成功能在 Swift 2.3 和 Swift 3.0 项目中都有效,但图像文字只能在 Swift 3 项目中正常工作。

Give it a try in a Swift 3 playground and you'll find it works just fine.

在 Swift 3 playground 试一试,你会发现它运行得很好。

enter image description here

在此处输入图片说明

回答by Hong Wei

I have been using this feature. Basically if I have an image that begins with "ic", I would type let i =followed by ic. At this point, Xcode 8 will show a dropdown of images and I then choose the correct image from the list. I have never bothered to actually type UIImage(. It works just like autocompleting code except that it uses the filename of the image.

我一直在使用这个功能。基本上,如果我有一个以“ic”开头的图像,我会输入let i =后跟ic. 此时,Xcode 8 将显示图像下拉列表,然后我从列表中选择正确的图像。我从来没有费心去实际打字UIImage(。它的工作方式与自动完成代码类似,只是它使用图像的文件名。

In your case, there is probably some other issue causing the compilation to fail.

在您的情况下,可能还有其他一些问题导致编译失败。