부모뷰에서 Counter클래스를 사용한 이유는?
import Foundation
import SwiftUI
class Counter{
var count = 1
}
struct StateBinding:View {
private var counter = Counter()
var body: some View {
BindingView(counter: counter)
Button("Add Value"){
counter.count += 1
print(counter.count)
}
}
}
struct BindingView:View {
private var counter:Counter
init(counter: Counter) {
self.counter = counter
}
var body: some View {
Text(String(counter.count))
}
}
import Foundation
import SwiftUI
struct StateBinding:View {
@State private var count = 1
var body: some View {
BindingView(count: $count)
Button("Add Value"){
count += 1
print(count)
}
}
}
struct BindingView:View {
@Binding var count:Int
var body: some View {
Text(String(count))
}
}