【发布时间】:2020-01-01 01:38:50
【问题描述】:
我正在尝试在 SwiftUI Form 中创建一个操作,以向我的数据集添加一个额外的 Child。当我尝试附加 newChild 时,出现错误:
不能对不可变值使用可变成员:“self”是不可变的
struct Child : Identifiable {
var id = UUID()
var birthday: Date
var name: String
}
struct ContentView: View {
var children : [Child] = []
var body: some View {
VStack {
Button(action: {
let newChild = Child(birthday: Date(), name: "Carl")
children.append(newChild)
}) {
Text("Add Child")
}
}
}
}
据我所知,我的数组 children 是可变的,那么为什么会出现此错误?
【问题讨论】:
标签: ios swift struct immutability swiftui