Iphone swift:在创建(?)imageview时,它期望'';分隔符错误

Iphone swift:在创建(?)imageview时,它期望'';分隔符错误,iphone,swift,Iphone,Swift,“doodle”是我的AssetCatalog图像集名称。 在simplemageview~doodle“)”行中,有一个错误“expected”,“separator”。我只是跟着我的书 您试图同时使用UIImage(命名:)初始值设定项和#imageLiteral您不能使用。您必须使用其中任何一种 我更喜欢imageLiteral,因为它在编译时检查文本的有效性,并且在Xcode中看起来更好。你应该这样做: import UIKit class ViewController: UIView

“doodle”是我的AssetCatalog图像集名称。
在simplemageview~doodle“)”行中,有一个错误“expected”,“separator”。我只是跟着我的书

您试图同时使用
UIImage(命名:)
初始值设定项和
#imageLiteral
您不能使用。您必须使用其中任何一种

我更喜欢
imageLiteral
,因为它在编译时检查文本的有效性,并且在Xcode中看起来更好。你应该这样做:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var simpleImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        simpleImageView.image = UIImage(named:"#imageLiteral(resourceName: "doodle")")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
simpleImageView.image = #imageLiteral(resourceName: "doodle")
如您所见,您不需要编写
UIImage(命名:)
,因为
#imageLiteral
表示
UIImage
对象

或者,使用
UIImage(名称:)
初始值设定项,如下所示:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var simpleImageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        simpleImageView.image = UIImage(named:"#imageLiteral(resourceName: "doodle")")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
simpleImageView.image = #imageLiteral(resourceName: "doodle")
要使用初始值设定项,只需传入所需图像名称的简单字符串。

试试这个

simpleImageView.image = UIImage(named: "doodle")

如果您喜欢图像文字,可以使用便利
init(imageLiteralResourceName:)
初始值设定项:

simpleImageView.image  = #imageLiteral(resourceName: "Apple.png")

我认为在
#imageLiteral
周围不应该有
。目前,图像文字在Xcode中有很多问题。它们转换为图像的方式使得修改它们非常困难。我更希望代码保持不变,并在其他地方看到图像,例如在页边空白处。@Sulthan你说的是什么意思看到其他地方的图像了吗"? 什么利润?你在jetbrains的编辑部工作过吗?例如,请参见中CSS编辑的图像。它不改变代码,但使用左边距显示颜色/图像。@Sulthan是的,我有,我确实认为在边距中显示图像更好。但不幸的是,Xcode没有这个功能,在这种情况下,我更喜欢图像文字,因为我经常输入错误的图像名称@정초이 如果你认为我的答案回答你的问题,请考虑通过点击那个复选标记来接受它!