ABOUT ME

Today
Yesterday
Total
  • Ch6. Intro to controls: Text & Image
    Ray Wenderlich/SwiftUI 2021. 2. 7. 20:08

    Ch6. Intro to controls: Text & Image

    Are modifiers efficient?

    Modifier는 새로운 view를 리턴한다. 정말 이게 최선일까?

    Modifier는 사용될 때마다 뷰를 전달받아 새로운 뷰를 만든다. Recursive한 동작이 일어나는 것이다.

    Modifier를 연속적으로 호출하면 뷰 안에 뷰 안에 뷰 안에 뷰가 있는 그런 구조다. 마트료시카 인형처럼 말이다

     

    언뜻보면 자원낭비처럼 보인다. 하지만 SwiftUI는 이 Stack을 효율적인 자료구조를 통해 flatten하여 뷰를 렌더링하고 있다.

    그러니까 modifier를 마음껏 써도 된다.

    Text("Welcome to Kuchi")
    	.background(Color.red)
    	.padding()
    
    Text("Welcome to Kuchi")
    	.padding()
    	.background(Color.red)
    

    위 두 코드는 서로 다른 결과물을 가진다.

    Image

    Image에 Modifier를 적용하는 과정

     

    'Ray Wenderlich > SwiftUI' 카테고리의 다른 글

    Ch12. Conditional Views  (0) 2021.02.14
    Ch11. Lists & Navigation  (0) 2021.02.07
    Ch9. State & Data Flow  (0) 2021.02.07
    Ch8. Introducint Stacks & Containers  (0) 2021.02.07
    Ch7. Controls & User Input  (0) 2021.02.07
Designed by Tistory.