View
는 구조체의 일종인데 구조체는 immutable
하기 때문! 따라서 modifier
와 같은 wrapper
에 의해 View
에 변화가 생기면, 수정본을 전시하는 게 아니라 변화를 반영한 새로운 View
를 전시한다!@State
가 붙은 프로퍼티는 해당 프로퍼티가 정의하고 있는 타입의 변수가 존재하는 구조체 외부의 별도 메모리를 가리키는 포인터
와 같으며, 실제 값의 변화는 해당 메모리에서 일어나는 것!
즉, 구조체 내부의 변수는 특정한 위치의 메모리를 가리키고 있으므로 변하지 않으며, 상태 변화는 해당 메모리 위치에서 발생한다! 뭔가 편법같지만...아무튼 그럼ㅋㅎㅋㅎㅋㅎ
identifier
가 있어야 배열의 각 요소와 연관된 view
가 무엇인지 기록할 수 있다! 남은 공간 전부
를 차지하는 방식으로 작동View builder
는 for문을 사용할 수 없는데 이를 대체하는 함수!@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 changedthe 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!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")
})
}
}