I keep receiving an error/lint which reads Variable 'self.item' used before being initialized. This message only appears when I seemingly add a @State of type Date (see commented line below).
Variable item is a CoreData value that I'm attempting to update through a form. All of the other required data types (int, string, data, etc.) all work as expected.
I'm fairly confident that this is an issue which stems from my lack of experience with Swift or declarative-style languages in general, but I'm also wary that it could be a compiler issue as I seem to run into a few of those as well.
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()
}
...
self._timestamp = State(initialValue: Date(timeIntervalSince1970: item.timestamp))$item.name,item.imageDataObservableproxy class with from and to methods in an extension of theNSManagedObjectsubclass. The benefit is a more convenient handling of the optionals and you don't need to insert a new object into the context if you also want to use the form for adding a new item.