当我在以下对象上链接方法时
let developerButton = UIButton(type: .system).titleLabel?.text = "developer"
我收到消息:
Constant 'developerButton' inferred to have type '()?', which may be unexpected
我不明白。有什么帮助吗?
最佳答案
您的 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)
关于swift - 常量 ... 推断为类型 '()?' ,这可能是意外的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46953604/