Swift 常数推断为具有类型';()?';,这可能出乎意料
当我在以下对象上链接方法时Swift 常数推断为具有类型';()?';,这可能出乎意料,swift,Swift,当我在以下对象上链接方法时 let developerButton = UIButton(type: .system).titleLabel?.text = "developer" 我得到的信息是: 常量“developerButton”推断为具有类型“()?”,可能是 意料之外 我不明白。有什么帮助吗?试试这个: let developerButton = UIButton(type: .system) developerButton.titleLabel?.text = "develope
let developerButton = UIButton(type: .system).titleLabel?.text = "developer"
我得到的信息是:
常量“developerButton”推断为具有类型“()?”,可能是
意料之外
我不明白。有什么帮助吗?试试这个:
let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"
试试这个:
let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"
您的
developerButton
是文本赋值text=“developer”
的结果,该赋值返回Void
(无,()
)。分配是有条件的,这就是为什么您会得到Void?
(或()?
)
正确分配两份作业
let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"
还请注意,您应该使用setTitle
,而不是直接设置标题文本:
let developerButton = UIButton(type: .system)
developerButton.setTitle("developer", for: .normal)
您的
developerButton
是文本赋值text=“developer”
的结果,该赋值返回Void
(无,()
)。分配是有条件的,这就是为什么您会得到Void?
(或()?
)
正确分配两份作业
let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"
还请注意,您应该使用setTitle
,而不是直接设置标题文本:
let developerButton = UIButton(type: .system)
developerButton.setTitle("developer", for: .normal)