【发布时间】:2017-04-24 09:54:11
【问题描述】:
今天我在长时间使用objective c时想到了一个问题。假设我有以下用 swift 3.0 编写的计算初始化程序代码。
private let loginButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(hex: 0x0083C5, alpha: 1)
button.setTitle("Login", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .bold)
button.layer.cornerRadius = 5
return button
}()
上面的代码返回一个 UIButton 的实例。如何在目标 C 中编写相同的代码?
更新:我已经知道使用延迟实例化的方式了。有没有其他方法可以创建不可变实例?
【问题讨论】:
-
类似的 C 构造是“复合语句表达式”,示例参见 stackoverflow.com/questions/21909511/…。
-
@MartinR - 我可以在这里使用相同的东西吗?
标签: ios objective-c swift