11.3. App11: Getting the Details of a Selected Contact (details endpoint)
The details endpoint sends us a response like this:
{
"email": "[email protected]",
"name": "Alice Smith",
"phone": 6781234567
}We already have a data type Contact to match this structure. We need to make it adopt the Codable protocol:
//
// Contact.swift
// App11
//
// Created by Sakib Miazi on 5/26/23.
//
import Foundation
//MARK: struct for a contact...
struct Contact: Codable{
var name:String
var email:String
var phone:Int
init(name: String, email: String, phone: Int) {
self.name = name
self.email = email
self.phone = phone
}
}
Now, let's update the getContactDetails(name: String) method:
In the above code:
In line 4, we are setting the URL for the details endpoint.
In line 5, we set the parameters to
["name": name].Between lines 21 to 28, we decode the data with JSONDecoder() using the struct
Contact. And then, we create an alert to display the details of the received contact.(You get to call this method from the adopted method,
didSelectRowAtrelated to table view).Between lines 56 through 68, we display an alert to show the details.
Now, if we run the app, we will see:

Last updated
Was this helpful?