Saving Data in iOS

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

Part 2: JSON

11. JSON Decoding

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: 10. Introduction Next episode: 12. Challenge: Decoding 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: 11. JSON Decoding

Apple’s documentation on Encoding and Decoding Custom Types

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

This video is where you’ll learn about the JSON file format. Or, as some other people pronounce it, “Jason”. Regardless of how you say it, JSON is currently a very popular format for transmitting data over the web. Working with JSON supplied by web services is a frequent task in app development.

// MARK: - Private Methods
private func loadJSON() {
}
guard let taskJSONURL = Bundle.main.url(forResource: <#T##String?#>, withExtension: <#T##String?#>)
"Task"
"json"
guard let taskJSONURL = Bundle.main.url(forResource: "Task", withExtension: "json"),
  let prioritizedTaskJSONURL = Bundle.main.url(forResource: "PrioritizedTask", withExtension: "json") else {
    return
}
let decoder = JSONDecoder()
let taskData = Data(contentsOf: taskJSONURL)
do {
    let taskData = try Data(contentsOf: taskJSONURL)
  } catch let error {
    print(error)
}
let prioritizedTaskData = try Data(contentsOf: prioritizedTaskJSONURL)
let task = try decoder.decode(<#T##type: Decodable.Protocol##Decodable.Protocol#>, from: <#T##Data#>)
print(task)
let task = decoder.decode(Tesk.self, from: <#T##Data#>)
let task = decoder.decode(Task.self, from: taskData)
let task = try decoder.decode(Task.self, from: taskData)
struct Task: Identifiable, Codable {
	...
}
print(task)
onAppear {
  self.loadJSON()
}
let prioritizedTask = try decoder.decode(TaskStore.PrioritizedTasks.self, from: prioritizedTaskData)
print(prioritizedTask)
struct PrioritizedTasks, Codable {
  ...
}