为什么我不能在 Xcode 8 中使用新的内联 #imageLiteral 语法?“返回结果中的预期表达式”

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

Why can't I use the new inline #imageLiteral syntax in Xcode 8? "Expected expression in return result"

iosswiftxcodeswift3xcode8

提问by Doug Smith

I'm trying to use the new #imageLiteralinline image assignment from your assets folder in Xcode 8, where instead of the whole UIImage(named: "")syntax it's just the image displayed, as shown here.

我正在尝试使用#imageLiteralXcode 8 中资产文件夹中的新内联图像分配,其中UIImage(named: "")仅显示图像而不是整个语法,如下所示

But when I do, I get the error "Expected expression in return result" when it's a function.

但是当我这样做时,当它是一个函数时,我会收到错误“返回结果中的预期表达式”。

Here's a simple example:

这是一个简单的例子:

func img() -> UIImage {
    return #imageLiteral(resourceName: "small-checkmark")
}

Where in Xcode it displays the small-checkmarkas would be expected, but it won't compile due to the above error. Everything is autocompleted for me, the file is in the assets folder (a PDF). I don't understand what's going wrong. If I just return UIImage()it compiles fine.

它在 Xcode 中的哪个位置small-checkmark按预期显示,但由于上述错误而无法编译。一切都为我自动完成,文件位于资产文件夹(PDF)中。我不明白出了什么问题。如果我只是返回UIImage()它编译正常。

What should I be doing?

我该怎么办?

采纳答案by Kentaro Matsumae

You should check Swift Version.

您应该检查 Swift 版本。

Build Settings > Use Legacy Swift Language Version = NO

Image Literal is only supported on Swift3.0, not Swift2.3.

Image Literal 仅支持 Swift3.0,不支持 Swift2.3。

回答by LukeSideWalker

For all having the same problem with Xcode 10 / Swift 4.2 -> Unfortunately, this feature (imageLiterals, auto completion ) was deprecated in its previous form.

对于所有在 Xcode 10 / Swift 4.2 中遇到相同问题的人 -> 不幸的是,此功能(imageLiterals,自动完成)在其以前的形式中已被弃用。

But there is new way to use imageLiterals -> https://stackoverflow.com/a/52523111/3143890

但是有使用 imageLiterals 的新方法 -> https://stackoverflow.com/a/52523111/3143890