I am learning SwiftUI by building an app that uses a form with a TextField and imagePicker. Used two sets of codes, TextField and imagePicker together but don't know how to consolidate saveImage with Add/Save Button of fileData.writeData to save both data of TextField and image into Core Data at same time. Here are the codes:
@ObservedObject var fileData : FileVM
@Environment(\.managedObjectContext) var moc
@Environment(\.managedObjectContext) var context
@State private var image: Image?
@State private var showImagePicker = false
@State private var inputImage: UIImage?
...
Form {
HStack {
Text("File Name:")
Spacer()
TextField("required", text: $fileData.fileName)
...
}
HStack {
Text("Attachments: ")
...
Button(action: { self.showImagePicker.toggle() }) {
Text(" \(Image(systemName: "photo")) ")
}
}
Button(action: saveImage) { // the function of this button should consolidated in the Add/Save button below.
Text("Save")
...
}
...
Button(action: {fileData.writeData(context: context)}, label: {
title: do { Text(fileData.updateFile == nil ? "Add" : "Save")
...
func saveImage() {
let jpegPickedImage = inputImage?.jpegData(compressionQuality: 1.0)
let fileimages = FilesEnt(context: self.moc)
fileimages.fileImages = jpegPickedImage
try? self.moc.save()
print("Image is saved")
}
func loadImage() {
guard let inputImage = inputImage else { return }
image = Image(uiImage: inputImage)
}
func writeData(context : NSManagedObjectContext) {
if updateFile != nil {
updateFile.fileName = fileName
updateFile.fileImages = fileImages
try! context.save()
updateFile = nil
isNewData.toggle()
fileName = ""
fileImages = Data()
return
}
let newFile = FilesEnt(context: context)
newFile.fileName = fileName
newFile.fileImages = fileImages
do {
try context.save()
isNewData.toggle()
fileName = ""
fileImages = Data()
}
catch{
print(error.localizedDescription)
}
}
Thanks for help!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
An app of filing that has a form with a Picker and two entities behind, one-many relationship. Want to save a Picker value picked from one entity and save into the other, specifically a value picked from cateName.CatesEnt saved into fileName.FilesEnt via a TextField. Tried with the code below but got a yellow stopper error.
struct FileAddEdit: View {
@ObservedObject var fileData : FileVM
@Environment(\.managedObjectContext) var context
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \CatesEnt.cateOrder, ascending: true)], animation: .default)
private var cates: FetchedResults<CatesEnt>
@State var selectedCate: CatesEnt? = nil
@State var cateName: [CatesEnt]
...
HStack {
Text("File Name:")
TextField("required", text: $fileData.fileName)
}
HStack {
Text("Folder:")
TextField("select a folder", text: $fileData.fileCate)
.onTapGesture {
Picker(selection: $selectedCate, label: Text("folder: ")) {
// yellow stopper waning: "Result of 'Picker <Label, SelectionValue, Content>' initializer is unused"
ForEach(cateName) { cate in
Text(cate.cateName ?? "").tag(cate as CateEnt?)
}
}
}
}
...
also added those lines in CatesEnt+ & FilesEnt+CoreDataProperties.swift files:
public var wrappedCateName: String {
cateName ?? "unknown cates"
}
public var wrappedFileName: String {
fileName ?? "unknown files"
}
public var wrappedFileCate: String {
fileCate ?? "unknown files"
}
public var catesArray: [CatesEnt] {
let set = cates as? Set<CatesEnt> ?? []
return set.sorted {
$0.wrappedCateName < $1.wrappedCateName
}
}
in another view, user enters cateName values that are shown as a list in the same order in Picker.
I am rockie learning SwiftUI. Thanks for help!