简体   繁体   中英

Xcode 8 Image Completion

In Xcode 8, you have images autocompleted while typing.

Question is : Why?

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

里面的UIImage 外部UIImage

Does anyone know how to use it ?

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.

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 i when you want to set an image.

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 #imageLiteral syntax is only recognised on Swift 3 or later.

Here is a demonstration video link where I do this and here is a link to a sample project that I created.

Note that 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.

Update 1

This functionality remains in Xcode Version 9.0 beta 6 (9M214v) 在此处输入图片说明

Update 2

Xcode 11.2.1 , this is not appearing anymore.

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

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.

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

在此处输入图片说明

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM