Lecture 2: Learning more about SwiftUI

sun·2021년 9월 22일
1

youtube link

# 왜 View는 변경사항이 있을 때 마다 수정본이 아니고 아예 새로운 버전을 만들까?

  • View 는 구조체의 일종인데 구조체는 immutable 하기 때문! 따라서 modifier 와 같은 wrapper 에 의해 View 에 변화가 생기면, 수정본을 전시하는 게 아니라 변화를 반영한 새로운 View 를 전시한다!

# 그런데 구조체 내부에서 쓰는 @State는 계속 값이 바뀌는 변수 아닌가...?

  • @State 가 붙은 프로퍼티는 해당 프로퍼티가 정의하고 있는 타입의 변수가 존재하는 구조체 외부의 별도 메모리를 가리키는 포인터 와 같으며, 실제 값의 변화는 해당 메모리에서 일어나는 것!

  • 즉, 구조체 내부의 변수는 특정한 위치의 메모리를 가리키고 있으므로 변하지 않으며, 상태 변화는 해당 메모리 위치에서 발생한다! 뭔가 편법같지만...아무튼 그럼ㅋㅎㅋㅎㅋㅎ


# 배열은 identifier가 필요하다

  • 배열의 요소는 추가/삭제 될 수 있어 계속 순서가 바뀌므로 각 요소마다 고유한 identifier 가 있어야 배열의 각 요소와 연관된 view 가 무엇인지 기록할 수 있다!
  • 주의할 점은 문자열은 고유한 Id 프로퍼티가 없으므로 중복되는 경우 이를 구분할 방법이 없다...

# spacer()

  • 남은 공간 전부 를 차지하는 방식으로 작동

# ForEach

  • View builder 는 for문을 사용할 수 없는데 이를 대체하는 함수!
    Q. 왜 사용이 불가능할까? view builder 뿐만이 아니라 모든 result builder에서 사용이 불가능한 것 같은데...

# 복잡함 멈춰

Q. var body에서 사용하는 변수를 왜 body 내부에서 선언하면 안될까?

  • 일단 이번주 리딩이랑 과제 하고 돌아와서 질문 해결하기!


💡 Before Reading !

  • contents below are unorganized notes i took while listening to the lecture. i decided to post the organized version in Korean(b/c cs193p is awesome but not well-known here in Korea, so I wanted to kinda introduce this!) but left these anyway for backup! plz take that into mind if you've bumped into this post!

# why can't a view be modified but return new views every single time?

  • why does a view keep returning new view? because structs are immutable! therefore when there's a change you simply create a new view to adapt to those changes and return it instead!

# @state

  • the variable marked with @state is simply pointing at a memory where the variable of the designated type is actually kept, and it is at that memory where the value is actually changed
  • to sum up, the variable in struct itself is not changing (because structs are immutable!) as it is always pointing at the same memory, it is the memory where the change takes place!

# arrays need identifiers!

  • this is because we need to keep track of which Views are related with each item in the array
  • we could append or delete items in the array and without identifiers to uniquely identify each item, the correlation would be all messed up

Question: Why can't i have the var emojicount inside the body variable like this?

  • tried it, builds, but nothing happens when i acutally click the button
var body: some View {
        var emojiCount = 6
        VStack {
            HStack {
                ForEach(emojis[0..<emojiCount], id: \.self) { emoji in // bag of lego View!
                    CardView(content: emoji)
                }
            }
            .padding(.horizontal)
            .foregroundColor(.red)
            
            Button(action: {
                emojiCount += 1
            }, label: {
                Text("Add Card")
            })
            
            Button(action: {
                emojiCount -= 1
            }, label: {
                Text("Remove Card")
            })
            
        }
    }
profile
☀️

0개의 댓글

관련 채용 정보