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

不想你离开。 提交于 2019-12-19 17:44:47

问题


I'm trying to use the new #imageLiteral inline 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.

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-checkmark as 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.

What should I be doing?


回答1:


You should check Swift Version.

Build Settings > Use Legacy Swift Language Version = NO

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




回答2:


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

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



来源:https://stackoverflow.com/questions/39988566/why-cant-i-use-the-new-inline-imageliteral-syntax-in-xcode-8-expected-expres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!