I'm looking to save an image from PhotosPicker
to CoreData
(as a Binary Data with Allows External Storage) but I have an issue. I don't know it's default value.
I'm fine with other types but definitely not with Data, here's an example:
import SwiftUI
struct ContentView: View {
@State var value: Double = 0
@State var title: String = ""
@State var image: Data? = nil
var body: some View {
Button("Add", action: {
Persistence.shared.add(value: value, title: title, image: image ?? /* Something here */, context: viewContext)
})
}
}
1 - As said by Rob, the default value of type Data is just Data()
. I don't know why it tooks me so long to understand it. For the record, if you look at what it means for CoreData to store Data()
you will be able to see that there is almost nothing. It's very interesting. But it's not nil
as well.
2 - The result is like that:
Persistence.shared.add(value: value, title: title, image: image ?? Data(), context: viewContext)