我一直收到一个错误/lint,内容为Variable 'self.item' used before being initialized
。仅当我似乎添加了 @State
类型的 Date
时才会出现此消息(请参阅下面的注释行)。
变量 item
是我试图通过表单更新的 CoreData
值。所有其他必需的数据类型(整数、字符串、数据等)都按预期工作。
我相当有信心这是一个问题,因为我缺乏 Swift 或声明式语言的经验,但我也担心这可能是编译器问题,因为我似乎遇到了还有一些。
import SwiftUI
struct SelectionView: View {
@State var item : Item
@Environment(\.managedobjectcontext) private var viewContext
@State private var name : String
@State private var imageData : Data
@State private var timestamp : Date // error only appears when this line is added
init(item : Item) {
self.item = item
self._name = State(initialValue: item.name!)
self._imageData = State(initialValue: item.imageData!)
self._timestamp = State(initialValue: item.timestamp!)
}
var body: some View {
VStack {
Form
{
TextField("Name",text: $name)
Image(uiImage: UIImage(data: imageData)!)
Button(action: changeImage,label: { Text("Change image") })
Button(action: save,label: { Text("Save") })
}
}
.padding()
}
...