Lazy - siwonkim0/ios-project-manager GitHub Wiki
Lazy ν€μλ μ¬μ©μ λν κ³ λ―Ό
private lazy var entireStackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [titleLabel, totalCountLabel, spacerView])
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .fill
stackView.spacing = Design.entireStackViewSpacing
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
Lazy ν€μλμ μ¬μ© μ©λλ μ²μλΆν° λ©λͺ¨λ¦¬μ μ¬λ¦¬μ§ μκ³ , νΈμΆμ λ©λͺ¨λ¦¬μ μ¬λ¦¬κΈ° μν΄ μ¬μ©νλ κ²μ΄λ€.
Lazy ν€μλλ₯Ό μ¬μ©μ μ£Όμν μ μ λν΄μ κ³ λ―Όν΄λ³΄μλ€.
λ©λͺ¨λ¦¬ λμ
첫λ²μ§Έλ‘ λ³μμ ν΄λ‘μ μ μ€ν κ²°κ³Όλ₯Ό ν λΉνλ κ²½μ°λ λ©λͺ¨λ¦¬ λμμ μνμ΄ μλ€.
λλ²μ§Έλ‘ λ³μμ νμ μ ν΄λ‘μ λ‘ μ€μ νμ¬ ν΄λ‘μ μ€νμ κ²°κ³Όκ° μλ ν΄λ‘μ μ체λ₯Ό λ΄κ³ μλ λ³μλΌλ©΄ λ©λͺ¨λ¦¬ λμμ μνμ±μ΄ μμ΄μ [weak self]λ‘ λ©λͺ¨λ¦¬ λμλ₯Ό λ°©μ§ν΄μ€μΌνλ€.
lazy var greeting: String = {
return "Hello my name is \((self.name))"
}()
lazy var greeting: () -> String = { [weak self] in
return "Hello my name is \(((self?.name))!)"
}
μ¬μ©μ μ£Όμν μ μ λ§μΌ ν΄λΉ ν΄λ‘μ μμ selfλ₯Ό μ°Έμ‘°ν μ λ©λͺ¨λ¦¬ λμκ° λ°μν μ μλ€λ μ μ΄ μλ€.
λν swift μ½λλ κΈ°λ³Έμ μΌλ‘ thread-safe νμ§ μκΈ° λλ¬Έμ μ¬λ¬ μ€λ λκ° λμμ ν΄λΉ lazy var λ³μμ μ κ·Όνλ€λ©΄ ν΄λΉ lazy λ³μκ° μ¬λ¬λ² μμ±λ μνμ΄ μμΌλ―λ‘ lazy var λ λ©ν° μ€λ λ νκ²½μμ μ κ·Όνλ©΄ μλλ€.
μ μ¬νλ€μ λν΄ κ³ λ―Όν΄λ³Έ κ²°κ³Ό ν΄λΉ μ½λλ λ©λͺ¨λ¦¬ λμλ μΌμ΄ μμμ νμΈνμκ³ , lazyμ μ©λμ λ¬λ¦¬ stackViewλ μ²μλΆν° λ©λͺ¨λ¦¬μ μ¬λ €μΌλλ λ·°μ΄λ―λ‘ μΈ νμκ° μμ΄λ³΄μ¬ μ κ±°νμμ
private let entireStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .fill
stackView.spacing = Design.entireStackViewSpacing
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()