How to use a Codable struct to write to root.plist

82 Views Asked by At

I have a file structure that looks like this:

enter image description here

I'm using a struct that conforms to the Codable protocol. I want to write it's values to the Root.plist but I don't know how to do this. I read from the Root.plist file like this:

let defaults = UserDefaults.standard.dictionaryRepresentation()
let systemID = defaults["systemID"] as? String ?? "Demo"

I don't want to write to this file one property at a time. This is what I'm trying but it says that the Root.plist doesn't exist.

func writeToSettingsBundle(config: Configuration) {
        let encoder = PropertyListEncoder()
        encoder.outputFormat = .xml
        
        let path = FileManager.default.urls(for: .applicationDirectory, in: .userDomainMask)[0].appendingPathComponent("Root.plist")
        
        print("path: \(path)")
        
        do {
            let data = try encoder.encode(config)
            try data.write(to: path)
        } catch {
            print("Could not encode settings: \(error.localizedDescription)")
        }
    }

I"m new to iOS.....what am I doing wrong?

0

There are 0 best solutions below