iOS/🟠 Swift

[Swift] didSet 이 ν˜ΈμΆœλ˜λŠ” μ“°λ ˆλ“œλŠ”?

woozzang 2022. 11. 6. 20:14

κ²°λ‘ 

- setter κ°€ ν˜ΈμΆœλ˜λŠ” μ“°λ ˆλ“œμ—μ„œ didSet 이 ν˜ΈμΆœλœλ‹€.

- setter κ°€ λ‚΄λΆ€ μ½”λ“œμ—μ„œ λ§ˆμ§€λ§‰μ— didSet 을 ν˜ΈμΆœν•˜λŠ” κ²ƒμœΌλ‘œ 보인닀.

- setter λŠ” κ°’ μ„€μ • μ½”λ“œκ°€ μ‹€ν–‰λœ 것이닀. (μ˜ˆμ‹œ: text = "abc")

- setter κ°€ λ§Œμ•½ main thread 이 μ•„λ‹Œ μ“°λ ˆλ“œ μ—μ„œ μ‹€ν–‰λ˜μ—ˆλ‹€λ©΄, didSet 도 ν•΄λ‹Ή μ“°λ ˆλ“œ μ—μ„œ μ‹€ν–‰λœλ‹€.

 

κ·Έλž˜μ„œ?

- didSet 내뢀에 UI μ„€μ • μ½”λ“œλ₯Ό λ„£κ³  μ‹€ν–‰ν•˜λ©΄ Main Thread Checker 에 μ˜ν•΄ κ²½κ³ λ©”μ‹œμ§€λ₯Ό λ§Œλ‚  수 μžˆλ‹€.

- UI μž‘μ—…μ€ main thread μ—μ„œ μ‹€ν–‰λ˜μ–΄μ•Όν•˜λ©°, didSet 은 main thread μ—μ„œ μ‹€ν–‰λ˜λŠ” 것을 보μž₯ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ΄λ‹€.

 

- 이 λ•ŒλŠ” ν•΄λ‹Ή μ½”λ“œκ°€ λ©”μΈμ“°λ ˆλ“œμ—μ„œ μ‹€ν–‰λ˜λ„λ‘ 보μž₯ν•˜κ±°λ‚˜, property observer λ‚΄λΆ€μ—μ„œλŠ” μž‘μ„±ν•˜μ§€ μ•ŠμœΌλ©΄ λœλ‹€.

    var text: String = "" {
        didSet {
            DispatchQueue.main.async { [weak self] in
                self?.label.text = text
            }
        }
    }

 

Property observer λ₯Ό μ •λ¦¬ν•œ κΈ€

https://marsh-flavor-e1c.notion.site/Property-Observers-707524ab1fd641a58565a3519657fd09

 


μ˜€λŠ˜λ„ λ‚˜λŠ” μ„±μž₯ν–ˆλ‹€!!πŸ”₯

 

πŸ€– [Back to the Basics] πŸ’»
μ£Όλ‹ˆμ–΄ iOS 개발자 우짱의 기술 λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.

 

[μ°Έκ³  자료 πŸ™‡πŸ»‍♂️]

thanks to. ν˜ΈμŠΉλ‹˜

https://docs.swift.org/swift-book/LanguageGuide/Properties.html

https://developer.apple.com/documentation/xcode/diagnosing-memory-thread-and-crash-issues-early