<aside> <img src="notion://custom_emoji/b483512c-e260-41d3-b5ff-36d0dc5fa090/169e981a-0297-8019-be6b-007aa264a5c0" alt="notion://custom_emoji/b483512c-e260-41d3-b5ff-36d0dc5fa090/169e981a-0297-8019-be6b-007aa264a5c0" width="40px" />
Welcome to the Working with JSON workshop! We’re excited to help you on your journey to becoming an iOS developer! 🥳
In this stage of developing the AggieEats App, we will learn how to incorporate JSON into our application. This will allow us to efficiently load a large amount of data from a single file.
If you haven’t already, make sure you have Xcode 16 or higher on your Mac (instructions on how to download Xcode are in the developer-resources
channel on our discord).
Then, download the starter code from our GitHub Repository down below and open the AggieEats.xcodeproj
file with Xcode.
</aside>
GitHub - Swift-Coding-Club-UCD/BuildingYourFirstiOSApp
<aside> <img src="notion://custom_emoji/b483512c-e260-41d3-b5ff-36d0dc5fa090/169e981a-0297-8019-be6b-007aa264a5c0" alt="notion://custom_emoji/b483512c-e260-41d3-b5ff-36d0dc5fa090/169e981a-0297-8019-be6b-007aa264a5c0" width="40px" />
JSON (JavaScript Object Notation) is a lightweight data format used to structure and transmit data. In Swift, JSON is commonly used for storing structured data like menus, user information, or API responses.
Swift makes handling JSON easy with its Codable
protocol, which allows us to encode and decode JSON objects into Swift models effortlessly.
</aside>
<aside> <img src="notion://custom_emoji/b483512c-e260-41d3-b5ff-36d0dc5fa090/169e981a-0297-8019-be6b-007aa264a5c0" alt="notion://custom_emoji/b483512c-e260-41d3-b5ff-36d0dc5fa090/169e981a-0297-8019-be6b-007aa264a5c0" width="40px" />
Let’s import the OrderedCollections
framework, which enables us to use ordered data structures such as an OrderedDictionary
. Make sure to also add the OrderedCollections
package using the Swift Package Manager.
Now, we can make a Menu
struct that conforms to Codable
protocol so that Swift knows how to convert JSON into a Menu
object. This struct also conforms to the Hashable
protocol so that we can iterate over the menu for each weekday of the week.
</aside>
//
// MenuModel.swift
// AggieEats
//
import Foundation
import OrderedCollections
struct Menu : Codable, Hashable {
let id = UUID()
let day: String
let locationName: String
let coordinate: [Double]
let menu: OrderedDictionary<String, [String]>
}