Swift51.com
Swift 头像
Swift  2017-03-10 21:37

Swift初始化语法糖库Then

回复:0  查看:5893  感兴趣:72  赞:0  

Swift初始化语法糖库Then,使初始化代码更简洁。

使用Then初始化UILabel并设置其属性:

let label = UILabel().then {
  $0.textAlignment = .center
  $0.textColor = .black
  $0.text = "Hello, World!"
}
这相当于:

let label: UILabel = {
  let label = UILabel()
  label.textAlignment = .center
  label.textColor = .black
  label.text = "Hello, World!"
  return label
}()

相关开源代码