Intro to iOS Development
  • Welcome
    • ☀️Introduction
    • 🐣Getting Started
    • 🐦Course Staff
  • Logistics
    • 📜Syllabus
    • 🗓️Schedule
    • 🖊️Grading
    • 🙋‍♂️Office Hours
    • Ed Discussion
  • Assignments
    • 🍼A1: Swift Basics
    • 🧑A2: Profile
    • 💬A3: ChatDev
    • 👨‍🍳A4: ChefOS
    • 👨‍🍳A4: ChefOS - SwiftUI
    • 📱Hack Challenge
      • 🏆FA23 Winners
  • Lectures
    • 1️⃣Logistics + Swift Basics
    • 2️⃣UIKit + AutoLayout
  • 3️⃣MVC + Navigation + Delegation
  • 4️⃣UITableView
  • 5️⃣UICollectionView
  • 6️⃣Networking I
  • 7️⃣Networking II
  • 8️⃣SwiftUI I
  • 9️⃣SwiftUI II
  • 🔟Persistence + SnapKit
  • 🔔(11) Notifications
  • 🌎(12) Deployment and MapKit
  • Chapters
    • 🖐️Introduction
    • ☁️Git + GitHub
      • 1️⃣Git Installation
      • 2️⃣Git Basics
      • ➕Git+
    • 🐣Swift Basics
      • 1️⃣Variables and Constants
      • 2️⃣Data Types
      • 3️⃣Operators
      • 4️⃣Data Structures
      • 5️⃣Conditionals
      • 6️⃣Loops
      • 7️⃣Functions
      • 8️⃣Closures
      • 9️⃣Optionals
    • 🧰UIKit + AutoLayout
      • 1️⃣Classes
      • 2️⃣UIKit
      • 3️⃣AutoLayout
    • 📺MVC + Navigation + Delegation
      • 1️⃣MVC
      • 2️⃣Navigation
      • 3️⃣Delegation
    • 🏓UITableView
      • 1️⃣What is a UITableView?
      • 2️⃣UITableView Setup
    • 📚UICollectionView
      • 1️⃣What is a UICollectionView?
      • 2️⃣UICollectionView Setup
    • 🌐Networking I
      • 1️⃣HTTP Requests
      • 2️⃣Callbacks
      • 3️⃣Codable
    • 🌍Networking II
      • 1️⃣Alamofire
      • 2️⃣GET Requests
      • 3️⃣POST Requests
      • 4️⃣URLSession
    • 💾Persistence + SnapKit
      • 1️⃣Persistence
      • 2️⃣SnapKit
    • 🕊️SwiftUI
      • 1️⃣Introduction to SwiftUI
      • 2️⃣Getting Started with SwiftUI
      • 3️⃣Views + Modifiers
      • 4️⃣Layouts
      • 5️⃣Navigation
      • 6️⃣Property Wrappers
    • 🎛️Widgets
      • 1️⃣Introduction to Widgets
      • 2️⃣Setting Up Widgets
      • 3️⃣Building Widgets
      • 4️⃣Configuring Widgets
    • 🧱Project Foundation
    • ✅Testing
      • 1️⃣Unit Testing
    • 👣Debugging
      • 1️⃣OSLog
      • 2️⃣Crashlytics
      • 3️⃣Analytics
    • ☁️CI/CD
      • 1️⃣Xcode Cloud
      • 2️⃣AppStore Shipping
  • Guides
    • 🔨Xcode Project Setup
    • 🎨Figma
    • 📬Postman
    • 🥥CocoaPods
    • 🧰UIKit Handbook
    • 📑Tab Views
      • UITabBarController
      • TabView
  • Work in progress
    • 🧵Concurrency
    • 2️⃣UI Testing
    • 🕐Reactive Programming
    • 🧠Memory Management
      • 🔁ARC
    • 📦Storage
    • 📣Notifications
  • Archived
    • SP24
      • Logistics
        • 📜Syllabus
        • 🗓️Schedule
        • 🖊️Grading
        • 🙋‍♂️Office Hours
      • Assignments
        • 🍼A1: Swift Basics
        • 🧑A2: Profile
        • 💬A3: ChatDev
        • 👨‍🍳A4: ChefOS
        • 📱Hack Challenge
          • 🏆FA23 Winners
      • Lecture
        • 0️⃣Course Intro + Logistics + Git Setup
        • 1️⃣Swift Basics
        • 2️⃣UIKit + AutoLayout
        • 3️⃣MVC + Navigation + Delegation
        • 4️⃣UITableView
        • 5️⃣UICollectionView
        • 6️⃣Networking I
        • 7️⃣Networking II
        • 8️⃣Persistence + SnapKit
        • 🔟SwiftUI
        • 🔢TabViews
    • FA23
      • Logistics
        • 🐣Getting Started
        • 🐦Course Staff
        • 📜Syllabus
        • 🗓️Schedule
        • 🖊️Grading
        • 🙋‍♂️Office Hours
      • Assignments
        • 🍼A1: Swift Basics
        • 🧑A2: Profile
        • 💬A3: ChatDev
        • 👨‍🍳A4: ChefOS
        • 📱Hack Challenge
          • 🏆FA23 Winners
      • Lectures
        • 0️⃣Course Intro + Logistics
        • 1️⃣Swift Basics
        • 2️⃣UIKit + AutoLayout
        • 3️⃣MVC + Navigation + Delegation
        • 4️⃣UITableView
        • 5️⃣UICollectionView
        • 6️⃣Networking I
        • 7️⃣Networking II
        • 8️⃣Persistence + SnapKit
        • 🎛️Widgets
          • 👼Introduction to Widgets
          • ⚒️Setting Up Widgets
          • 🧱Building Widgets
          • 👨‍💻Configuring Widgets
        • 🔟SwiftUI
    • SP23
      • Logistics
        • Lecture Schedule
        • Syllabus
        • Grading
        • SP23 Office Hours
      • Chapters
        • 1. Intro to Swift & Xcode
          • Lecture Handout
          • Lecture Demo
          • 🍼Project: Swift Basics
        • 2. UIKit and AutoLayout
          • Lecture Handout
          • Lecture Demo
          • 🛒Project: UIKit + AutoLayout
        • 3. Navigation, MVC, and Delegation
          • Lecture Handout
          • Lecture Demo
          • Project: Navigation + Delegation
        • 4. UITableView
          • Lecture Handout
          • Lecture Demo
          • Project: UITableView
        • 5. UICollectionView
          • Lecture Handout
          • Lecture Demo
          • Project: UICollectionView
        • 6. Networking I
          • Lecture Handout
          • Lecture Demo
          • Project: Persistence
        • 7. Networking II
          • Lecture Handout
          • Lecture Demo
            • Message Board
          • Project: Networking II
        • 8. Swift UI
        • 9. CocoaPods
          • 🍫Lecture Handout
          • 🧑‍🍳Lecture Demo
      • Cheat Sheets
        • Setting Up a New Xcode Project
        • Submitting Your Projects
        • Setting Up CocoaPods
    • 2022
      • SwiftUI
    • 2021
      • Adding Flare
      • Project: UIView Animations (Optional - Extra Credit)
      • UIView Animations
      • Xcode Tips and Tricks
    • 2019
      • Firebase
      • Persistence: UserDefaults
  • Swift Guide (ARCHIVED)
    • About this Textbook
    • Documentation
    • Constants and Variables
      • Variable Properties
      • Lazy and Static Variables
    • Functions
    • Ranges
    • Arrays
      • Basic Array Operations
      • Iteration and Enumeration
      • Advanced Array Operations
    • Tuples
    • Conditions and While Loops
    • For Loops
    • Enums and Switches
      • Enums with Associated Values
      • Indirect Enums
    • Classes and Structs
    • Optionals
    • Dictionaries
      • Dictionary Implementation
    • Closures
    • Constraints
    • Generics
    • Protocols
      • Protocols With Associated Types
    • Casting
    • Errors
    • Networking
      • Result
    • Inout
Powered by GitBook
On this page
  • Networking & Codable
  • Decoding Responses from the Internet
  • JSON Decoding
  • Using Your Decoded Data: @escaping completions
  • Making Network Calls
  • PDF Download

Was this helpful?

Export as PDF
  1. Archived
  2. SP23
  3. Chapters
  4. 7. Networking II

Lecture Handout

This lecture covers the second half of networking: decoding a response from the Internet.

Networking & Codable

Last week we talked about how to make HTTP requests to allow your app to communicate with the Internet. Today we'll talk about how to decode the responses and put them on the screen!

Decoding Responses from the Internet

Starting in Swift 4, there’s a protocol called Codable that objects, structs, and enums can conform to to make them encodable and decodable from JSON. Codable items can contain an enum for the coding keys of type String and CodingKey in order to decode the values from JSON.

We'll refer to the JSON response example from last lecture again today:

{
  "title": "HTTP, Networking, and JSON",
  "year_created": 2020,
  "success": true,
  "topics": [ "HTTP", "Networking", "JSON" ],
}

If we wanted to decode this example JSON response into a model of type Lecture, we could create a Lecture struct that conforms to Codable as follows:

struct Lecture: Codable {
  var title: String
  var yearCreated: Int
  var success: Bool
  var topics: [String]
}

Notice how the names of our variables match the fields in our JSON. This is done on purpose! With Codable, the names of the variables are expected to exactly match the keys in the JSON response.

JSON Decoding

In order to decode the JSON response into the Lecture class, we can create an instance of JSONDecoder, and then call the decode method to convert it from a JSON to a Lecture object.

// Assume we already received the data from an Alamofire call, stored in a variable named responseData

// Create an instance of a JSONDecoder
let jsonDecoder = JSONDecoder()

// Decode the data as a Lecture (optional type)
let lecture = try? jsonDecoder.decode(Lecture.self, from: responseData)

Note: Since the decoding can throw errors, we precede the decode method by a try?, which will make the lecture constant an optional value. This means that lecture will either contain a Lecture item or it could be nil.

Using Your Decoded Data: @escaping completions

Once you’ve decoded the lecture JSON as a Lecture item, how do you use it? Since you're downloading this JSON from the Internet, it might take forever to load, and you only want to display this information on the screen once it’s ready (and possibly use a loading indicator in the meantime as the information loads). We also don’t want our application to just stall and wait until we get a response to do something else.

This is where a completion handler comes in handy. A completion handler is an argument passed into whatever networking function you make.

The completion handler will:

  1. Take in your decoded object(s)

  2. Pass them to the ViewController (or wherever you call this networking function from)

  3. Run some block of code that you write (ex: reload the view controller with the fetched data on the screen)

Back to the Lecture class example from before. Suppose we want to make a function that makes the network call and returns to us the Lecture JSON decoded as a Lecture item. Here’s an example of what that would look like:

static func getLecture(completion: @escaping (Lecture) -> Void) {
  Alamofire.request(endpoint, method: .get).validate().responseData { response in
    switch response.result {
    case let .success(data):
      let jsonDecoder = JSONDecoder()
      
      if let lecture = try? jsonDecoder.decode(Lecture.self, from: data) {
        /* Here we're calling the completion function 
        (passed in as an argument to the network call) 
        on the lecture object we just decoded */
        completion(lecture)
      }
    case .failure(let error):
      print(error.localizedDescription)
    }
  }
}

This completion handler is marked as @escaping because it can "escape" from the networking function call itself and hand it off to whoever called the function -- it doesn’t need to go through every line inside the function. Don't worry too much about what this means though!

Here, you can see that completion is a function that takes in one argument which is of type Lecture and has a return type of Void. We only call completion when we know that the response succeeded and we actually have an actual Lecture object.

Also note that here, we are using the responseData method instead of the responseJSON method! This is important because responseData converts the response into a Data object which is what JSONDecoder needs to decode it.

Making Network Calls

It's good practice to create a NetworkManager class with static methods for every network call your app makes, with the endpoint URLs hidden to the public (in case it’s private information!). This means that the earlier getLecture function in the previous code snippet would be inside a class named NetworkManager.

Then, in your ViewController (or wherever you want to make a network call from), you can make a network request and handle it appropriately. For example, if you have a variable for lecture and need to reload the data of a table view after you get lecture data from the Internet, you would do it like so:

// In a view controller that has a lecture variable and tableView variable
func getClasses() {
  // Call the getLecture method in your NetworkManager
  NetworkManager.getLecture { lecture in
    self.lecture = lecture
    
    // We want to run any UI updates inside a DispatchQueue.main.async {...} block
    DispatchQueue.main.async {
      self.tableView.reloadData()
    }
  }
}

The { lecture in ... } is the escaping completion we defined in the NetworkManager earlier. In this case, we get the lecture variable only if we made the network call to the endpoint, received a success, received the JSON, and then decoded it. If (and only if) the network call succeeded, we run completion (the block of code from lines 5 through 10). This ensures that we receive the updated Lecture object only when it’s ready, so the screen isn’t frozen while the app is trying to execute all of these network request steps.

PDF Download

Previous7. Networking IINextLecture Demo

Last updated 5 years ago

Was this helpful?

92KB
L7-Networking II and Codable.pdf
pdf
Networking II & Codable