UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Saving more than one struct to FileManager

Forums > Swift

@3DCDN  

Would it be possible to save more than one struct to the FileManager and then be able to parse the data cleanly as needed? I am currently saving one struct (toDoItems) to the FileManager and would like to add another instance. Could I save it under a different filename? or would I need to structure the settings within the toDoItems struct?

I would be saving one "todos" file and then one "settings" file. The todos save instance is shown below:

func saveData() { let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first let documentURL = (directoryURL?.appendingPathComponent("todos").appendingPathExtension("json"))! let jsonEncoder = JSONEncoder() let data = try? jsonEncoder.encode(toDoItems) do { try data?.write(to: documentURL, options: .noFileProtection) } catch { print("Error...Cannot save data!!! See error:(error.localizedDescription)") } }

//Thanks in advance,

// Rich

2      

If this is JSON that is written and read only by your app, then you can do whatever you want. You just need to define your JSON and the structs you use to encode/decode it appropriately.

And a tip: When posting code to the forum, place three backticks ``` on the line before and three backticks ``` on the line after in order to nicely format the code. This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.

So, this:

func saveData() {
    let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    let documentURL = (directoryURL?.appendingPathComponent("todos").appendingPathExtension("json"))!
    let jsonEncoder = JSONEncoder()
    let data = try? jsonEncoder.encode(toDoItems)
    do {
        try data?.write(to: documentURL, options: .noFileProtection)
    } catch {
        print("Error...Cannot save data!!!See error:(error.localizedDescription)")
    }
}

instead of this:

func saveData() { let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first let documentURL = (directoryURL?.appendingPathComponent("todos").appendingPathExtension("json"))! let jsonEncoder = JSONEncoder() let data = try? jsonEncoder.encode(toDoItems) do { try data?.write(to: documentURL, options: .noFileProtection) } catch { print("Error...Cannot save data!!! See error:(error.localizedDescription)") } }

2      

You could change the func to which one you want to save

func saveData<T: Encodable>(for type: T, to path: String) {
    let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    let documentURL = (directoryURL?.appendingPathComponent(path).appendingPathExtension("json"))!
    let jsonEncoder = JSONEncoder()
    let data = try? jsonEncoder.encode(type)
    do {
        try data?.write(to: documentURL, options: .noFileProtection)
    } catch {
        print("Error...Cannot save data!!!See error: \(error.localizedDescription)")
    }
}

then to called it

saveData(for: toDoItems, to: "todos")

saveData(for: settingItems, to: "settings")

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.