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!