iOS/SwiftUI

SwiftUI) "@State"로 구조체의 프로퍼티를 변경하기 (feat. 프로퍼티 래퍼; Property Wrappers)

Dev.Andy 2023. 11. 14. 11:20

목차

    개념

    @State를 왜 쓰는가?

    구조체 안에서 프로퍼티 값을 변경하기 위해

    struct 기반의 SwiftUI에서 인스턴스는 immutable 하기 때문에

    @State 이전 @State 이후

    정의

    A property wrapper type that can read and write a value managed by SwiftUI.
    SwiftUI에 의해 관리되는 값을 읽고 쓸 수 있게 가능하는 프로퍼티 래퍼의 타입 중 하나

    State | Apple Developer Documentation

    프로퍼티 래퍼(Property Wrapper)?

    프로퍼티를 감싸는 "포장지"

    A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property. 
    프로퍼티 래퍼는 프로퍼티가 저장되는 방식을 관리하는 코드와 프로퍼티를 정의하는 코드의 사이에서 구분 층(a layer of separation)을 더한다.

    중복되는 프로퍼티 정의를 "포장지"로 분리하여 "재사용"

    When you use a property wrapper, you write the management code once when you define the wrapper, and then reuse that management code by applying it to multiple properties.
    프로퍼티 래퍼를 사용하면, 해당 래퍼에 정의한 관리 코드를 한 번만(once) 작성하고, 해당 관리 코드를 다양한 프로퍼티에 적용하여 재사용한다.