【问题标题】:Error sintax UIColor(CGColor: selectedButton?.layer.backgroundColor) Swift错误 sintax UIColor(CGColor: selectedButton?.layer.backgroundColor) Swift
【发布时间】:2018-06-03 04:58:45
【问题描述】:

我正在尝试学习 swift 教程,但我有最新版本,并且教程在以前的版本中。因为我对swift不太了解,所以无法解决语法问题,我开始了。

行: let color = UIColor(CGColor: selectedButton?.layer.backgroundColor)

错误:

可选类型 CGColor= 的值未展开;你的意思是用'!'要么 '?'将 selectedButton?.layer.backgroundColor 替换为 '(selectedButton?.layer.backgroundColor)!'

我已经替换了这个:

let color = UIColor(CGColor: (selectedButton?.layer.backgroundColor)!)

现在下一个错误:

'init(CGColor)'的模糊使用

【问题讨论】:

  • 您的代码中有错字。正确的方法是UIColor(cgColor:)。如果您不确定方法签名,您应该依赖 Xcode 的自动完成功能。
  • 谢谢!我可以解决这个错误。!

标签: swift uicolor cgcolor


【解决方案1】:

正确的做法是

if let color = selectedButton.backgroundColor {
    // use color in here

}

但出于教程目的,请使用

let color = selectedButton.backgroundColor!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 2018-08-31
    相关资源
    最近更新 更多