[
{
"id": "eccdf4b8-c9f6-4eeb-8832-28027eb70155",
"isActive": true,
"name": "Gale Dyer",
"age": 28,
"company": "Cemention",
"email": "galedyer@cemention.com",
"address": "652 Gatling Place, Kieler, Arizona, 1705",
"about": "Laboris ut dolore ullamco officia mollit reprehenderit qui eiusmod anim cillum qui ipsum esse reprehenderit. Deserunt quis consequat ut ex officia aliqua nostrud fugiat Lorem voluptate sunt consequat. Sint exercitation Lorem irure aliquip duis eiusmod enim. Excepteur non deserunt id eiusmod quis ipsum et consequat proident nulla cupidatat tempor aute. Aliquip amet in ut ad ullamco. Eiusmod anim anim officia magna qui exercitation incididunt eu eiusmod irure officia aute enim.",
"registered": "2014-07-05T04:25:04-01:00",
"tags": [
"irure",
"labore",
"et",
"sint",
"velit",
"mollit",
"et"
],
"friends": [
{
"id": "1c18ccf0-2647-497b-b7b4-119f982e6292",
"name": "Daisy Bond"
},
{
"id": "a1ef63f3-0eab-49a8-a13a-e538f6d1c4f9",
"name": "Tanya Roberson"
}
]
},]
import Foundation
struct User: Codable, Identifiable, Hashable {
struct Friend: Codable, Identifiable, Hashable {
var id: String
var name: String
enum CodingKeys: String, CodingKey {
case id
case name
}
}
var id: String
var isActive: Bool
var name: String
var age: Int
var company: String
var email: String
var address: String
var about: String
var registered: Date = Date()
var joinedDate: String {
registered.formatted(date: .numeric, time: .omitted)
}
var tags: [String] = []
var friends: [Friend] = []
enum CodingKeys: String, CodingKey {
case id, isActive, name, age, company, email, address, about, registered, tags, friends
}
}
func loadData() async {
// get the URL
guard let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json") else {
print("Invalid URL")
return
}
// fetch the data
do {
let (data, _) = try await URLSession.shared.data(from: url)
// decode data if fetched correctly
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
if let decodedUsers = try? decoder.decode([User].self, from: data) {
users = decodedUsers
}
} catch {
print("Invalid data")
}
}
.dateDecodingStrategy = .iso8601 this will allows us to assign Date of
this format to registered. User.self.
That was incorrect, it's true that inside JSON there's a User, another User and so on, but overall
file is an array of User(s), and my code needs to reflect that.@State private var users = [User]() inside the ContentView() where the
loadData() was placed. I made a List for users and placed a Text(user.name) to see if I get anything from my call.
.onAppear{loadData()} will not work in this scenario. onApper accepts only
synchronous function. That's why we must use .task {await loadData()},
the await keyword is an important element, letting Swift know that this function might sleep.
LazyVGrid.
.padding(.horizontal) to all the elements inside this view. This will create a small gap between the edges of the phone and your view.
.navigationTitle(user.name) to make it the biggest and most prominent text on the screen, who's profile are you currently look at.
.background(.ultraThinMaterial), this ensure maximum readibility on smaller text elements, since the background I used has varied tones and can be a bit distracting.
@State private var path = [User](), this will allow me to navigate between different screens. Then I added .navigationDestination(for: User.self) { user in UserView(user: user)}, next step is to wrap the VStack created in UsersView in NavigationLink(value: user).