Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[대황] Demystify SwiftUI

SeungHyun Hong edited this page Jul 22, 2024 · 2 revisions
  • Identity - 뷰의 정체성

    • 명시적 identity
      • 포인터 identity: UIKit 에서는 클래스이기에 포인터가 곧 identity가 된다
      • swiftUI는 데이터에 묶거나(identifiable), 명시적으로 지정해줄 수 있음
    • 구조적 identity: 암시적 Identity
      • 뷰 구조(타입)을 기반으로 자동으로 추론해낸다.
  • AnyView 대신 ViewBuilder

    • 브랜치마다 (if-else / switch) 다른 뷰를 반환하려면 ViewBuilder 사용
  • @State, @StateObject 는 뷰와 lifetime 이 같은 storage

  • ForEach 를 사용할 때 동적 데이터에 indicies를 쓰면 안되는 이유

    • identity 가 위치인 index 로 결정되기에 오류 발생
  • inert 모디파이어

    // 이런 형태보다는,
    if hasPadding {
    	Text("Hello World!")
    		.padding(10)
    } else {
    	Text("Hello World!")
    }
    // 이런 형태가 낫다
    Text("Hello World!")
    	.padding(hasPadding ? 10 : 0)
    	
    // inert modifier Sample
    opacity(1)
    padding(0)
    transformEnvironment(...) {}
    • 그래서 개인적으로 디자인시스템의 뷰 설계 시, ViewModifier 형태로 변형시에 넣는 값에 Optional 을 넣는 걸 선호하는 편

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /