Skip to content

feat: SSG_A1_W4 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: SSG_A1_W4 #11

wants to merge 1 commit into from

Conversation

ykbeeee
Copy link
Collaborator

@ykbeeee ykbeeee commented May 30, 2025

🌱 Challenge 정보

Guiding Questions

  1. C2에서 나는 @EnvironmentObject를 썼는데 어떤 역할이었을까?
  2. @EnvironmentObject의 정의는 무엇이며 어떤 코드들이랑 같이 사용될까?

Guiding Activities

  • GPT에게 물어보기
  • Swift 공식문서 탐방
    정의: A property wrapper type for an observable object that a parent or ancestor view supplies.

Q1: 여기에서 @EnvironmentObject가 작동하는 방식을 알려줘
Q2: 내 코드 외적으로 @EnvironmentObject를 쓸때 자주 같이 사용되는 코드들이 있을까?
image

  • SwiftUI에서 사용하는 의존성 주입(Dependency Injection) 방식 중 하나로, MistakeManager라는 공유 데이터 객체를 CalView 내부에서 사용하기 위해 언한 것.
    - @EnvironmentObject란?
    - SwiftUI에서 앱 전체 또는 상위 뷰에서 제공한 객체를 하위 뷰에서 접근할 수 있도록 하는 속성 래퍼.
    - 작동 방식
    - 1. 공유할 객체 선언: 작성되어 있는 MistakeManager는 ObservableObject를 채택한 클래스이다.
    - @published var 와 함께 쓰이고, @published 속성의 바뀌면 뷰가 자동으로 업데이트된다.
image - 2. 상위에서 주입
             - 주의점: 	@EnvironmentObject로 선언했으면 반드시 상위 뷰에서 .environmentObject()로 주입해야 합니다.

그렇지 않으면 앱이 실행 중 크래시 납니다.
image

        - 3. 하위 뷰에서 사용
image
        - 4. Preview에서도 사용: 반드시 주입해줘야 함. 
image

-@EnvironmentObject를 쓰는 이유
- 의존성 주입: 뷰 안에서 직접 객체를 만들지 않아도 된다.
- 데이터 공유: 여러 뷰에서 같은 데이터를 사용 가능
- 자동 업데이트: 내부 데이터(@published)가 바뀌면 UI 자동 반영

  • @StateObject와의 차이점: @StateObject는 View 내부에서 직접 생성하고 관리하기 때문에 소유자 View에서 사용된다.

자주 쓰는 조합

  1. @EnvironmentObject + ObservableObject + @published : MVVM형 상태 관리
  2. .environmentObject() + Preview: 뷰 미리보기용 필수코드
  3. @StateObject + .environmentObject(): 루트 View에서 객체 생성 후 자식에게 공유
  4. NavigationStack + .environmentObject(): 이동할 때 데이터 유지

📌 Check List

  • [ ]

✨ 나의 Finding & Synthesis

  • 알게 된 점 1.
  • 알게 된 점 2.
  • 알게 된 점 3.
  • 알게 된 점 4.

✅ 팀원 확인

  • 팀원 1: @username
  • 팀원 2: @username
  • 팀원 3: @username
  • 팀원 4: @username

⚡ 리뷰어 확인

  • 학습 내용 검토 완료
  • 추가 학습 필요 시 코멘트

⁉️ 새롭게 생긴 Curiosity

  1. 궁금한 점 1
  2. 궁금한 점 2

@@ -0,0 +1,6 @@
![[Screenshot 2025-05-29 at 1.08.03 PM.png]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용한 스크린샷 파일도 같이 올려주셔야 할 것 같습니다!

Copy link
Collaborator

@Ssunbell Ssunbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨어요 ㅜㅠㅜ
LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants