swift - 常量 ... 推断为类型 '()?' ,这可能是意外的

标签 swift

当我在以下对象上链接方法时

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/

相关文章:

ios - SWRevealController 滑动手势

objective-c - 在 Swift 中填充 NSRectPointer

swift - 在 Xcode11 中导入 Swift 包

ios - iPad 上的 Swift Playground 可以包含多个源文件吗?

ios - Swift Ints 和一些 double 工作但不是所有 double

swift - 使用Github时如何隐藏APPKey

objective-c - 与 mac osx 10.10 不兼容的 ARC SDK

ios - AVAudioEngine实时调频

ios - 弱引用的委托(delegate)变为零

Swift、allKeys、数组、字典、顺序