Thank you for any help. although this code isn't printing it (as iv'e rewrote multiple times I suspect that its not receiving the name of the original file which is saved like this
struct File1: Codable, Identifiable {
var id = UUID()
var title: String
var description: String
var date: Date
var preventible: Bool
var Option1: String
var Option2: String
var file1Date: Date
init(title: String, description: String, date: Date, file1Date: Date, preventible: Bool, Option1: String, Option2: String) {
self.title = title
self.description = description
self.date = date
self.preventible = preventible
self.Option1 = alternateHandling
self.Option2 = alternatePerspective
self.file1Date = reflectionDate
}
}
Creation:
import SwiftUI
struct File1View: View {
@State private var title = ""
@State private var description = ""
@State private var date = Date()
@State private var preventible = false
@State private var Option1 = ""
@State private var Option2 = ""
@State private var File2Date = Date()
@Environment(\.presentationMode) var presentationMode
static func saveFile1(_ file1: File1) {
let fm = FileManager.default
let documentsDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let file1sDirectory = documentsDirectory.appendingPathComponent("file1s")
let file1File = file1sDirectory.appendingPathComponent("\(file1.id).json")
do {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try encoder.encode(file1)
try fm.createDirectory(at: file1sDirectory, withIntermediateDirectories: true, attributes: nil)
try data.write(to: file1File, options: .atomic)
} catch {
print("Failed to save file1: \(error.localizedDescription)")
}
}
var body: some View {
NavigationView {
VStack {
TextField("Title", text: $title)
TextField("Description", text: $description)
DatePicker("Date", selection: $date)
VStack {
Toggle("Preventible", isOn: $preventible)
TextField("Option1", text: $Option1)
TextField("Option2", text: $Option2)
DatePicker("File2Date", selection: $File2Date)
}
.hidden()
Button("Save") {
let file1 = File1(title: title, description: description, date: date, File2Date: File2Date, preventible: preventible, Option1: Option1, Option2: Option2)
File1View.saveFile1(file1)
presentationMode.wrappedValue.dismiss()
}
}
}
}
}