Saving Data in iOS

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

Part 2: JSON

12. Challenge: Decoding JSON Arrays

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: 11. JSON Decoding Next episode: 13. Saving On Device

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: 12. Challenge: Decoding JSON Arrays

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've reached locked video content where the transcript will be shown as obfuscated text.

If your type is Encodable, not only can you decode an instance of it in JSON, you can store an array of its instances in JSON.

let tasksJSONURL = Bundle.main.url(forResource: "Tasks", withExtension: "json")
let prioritizedTaskJsSONURL = Bundle.main.url(forResource: "PrioritizedTasks", withExtension: "json")
let tasksData = try Data(contentsOf: tasksJSONURL)
let tasks = try decoder.decode(Task.self, from: tasksData)
print(tasks)

let prioritizedTasksData = try Data(contentsOf: prioritizedTasksJSONURL)
let prioritizedTasks = try decoder.decode(TaskStore.PrioritizedTasks.self, from: prioritizedTasksData)
print(prioritizedTasks)
let tasks = try decoder.decode([Task].self, from: tasksData)

let prioritizedTasks = try decoder.decode([TaskStore.PrioritizedTasks].self, from: prioritizedTasksData)
import Combine
import Foundation
private func loadJSONPrioritizedTasks
guard let tasksJSONURL = Bundle.main.url(forResource: "PrioritizedTasks", withExtension: "json") else {
  return
}
let tasksData = try Data(contentsOf: tasksJSONURL)
prioritizedTasks = try decoder.decode([PrioritizedTasks].self, from: tasksData)
@Published var prioritizedTasks: [PrioritizedTasks] = []
init() {
  loadJSONPrioritizedTasks()
}