Saving Data in iOS

May 31 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: JSON

14. JSON Encoding

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 13. Saving On Device Next episode: 15. Challenge: Encoding JSON Arrays

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 14. JSON Encoding

This course was originally recorded in April 2020. It has been reviewed and all content and materials updated as of November 2021.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

I’m 50% done with the whole “Forgetful Felipe” thing. I’m already remembering things thanks to the app loading stored data from the Document directory, but unfortunately it’s not saving any new things I don’t want to forget about.

private func saveJSONPrioritizedTask() {
  ...
}
let encoder = JSONEncoder()
do {
  
} catch let error {
  print(error)
}
let taskData = try encoder.encode(prioritizedTasks.first?.tasks.last)
let taskJSONURL = URL(fileURLWithPath: "Task",
                      relativeTo: FileManager.documentsDirectoryURL).appendingPathExtension("json")
try taskData.write(to: taskJSONURL, options: .atomicWrite)
@Published var prioritizedTasks: [PrioritizedTasks] = [] {
  didSet {
    saveJSONPrioritizedTask()
  }
}
let tasksJSONURL = URL(fileURLWithPath: "PrioritizedTasks",
                       relativeTo: FileManager.documentsDirectoryURL).appendingPathExtension("json")