0% found this document useful (0 votes)
36 views6 pages

Fetch Data Detail

Uploaded by

razzamishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

Fetch Data Detail

Uploaded by

razzamishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPANY NAME

Project Proposal
Prepared for: Anoop Mishra
Prepared by: Anoop Mishra iOS Developer
16 September 2022
Proposal number: 8299544315
COMPANY NAME

EXECUTIVE SUMMARY

Objective

How to fetch data from array type API in SwiftUI.

Goals
How to fetch data from array type API in SwiftUI.

Solution:

Please stop using SwiftyJSON. It's not topical anymore. Decode


the JSON directly into the model with JSONDecoder. And please
have a close look at the JSON. The key data is
inside BookDetails which is not the root object. By the
way .padding(.all,4) and .padding(4) does the same.
Project Outline
PRIMEITZEN SOFTWARE SOLUTION PVT. LTD

Try this solution code, with a working set of data model structs, and an
updated getData() function to fetch the data from the server. You still need to
check the server documentation, to determine which properties are
optional.

import Foundation
import SwiftUI

struct ContentView: View {


var body: some View {
PrimeBooksView()
}
}

class PrimeBookVM: ObservableObject {


@Published var datas = [Datum]()

init() {
getData()
}
func getData() {
guard let url = URL(string: "[Link]
else { return }
[Link](with: url) { (data, _, _) in
if let data = data {
do {
let results = try JSONDecoder().decode([Link], from: data)
[Link] {
[Link] = [Link]
}
}
catch {
print(error)
}
}
}.resume()
}
}
struct PrimeBooksView: View{
@StateObject var list = PrimeBookVM()

var body: some View{


ScrollView(.horizontal){
HStack {
ForEach([Link], id: \.self){ item in
VStack(alignment: .leading){
AsyncImage(url: URL(string: [Link])) { image in
image
.resizable()
.aspectRatio(contentMode: . t)
.frame(width: 180, height: 230)
} placeholder: {
ProgressView()
}
Text([Link])
.multilineTextAlignment(.leading)
.font(.system(size: 16))
Text([Link])
.font(.system(size: 12))
.fontWeight(.light)
}
.padding(4)
.background([Link]).cornerRadius(8)
.shadow(color: .gray, radius: 1)
}
}
}
}
}
fi
public struct ApiResponse: Codable {
let bookDetails: BookDetails
let bookSearch: String?
let uploadTypeID: Int
let stackID: String
let data: Int

enum CodingKeys: String, CodingKey {


case bookDetails, bookSearch
case uploadTypeID = "upload_type_id"
case stackID = "stack_id"
case data
}
}

public struct BookDetails: Codable {


let currentPage: Int
let data: [Datum]
let rstPageURL: String
let from, lastPage: Int
let lastPageURL, nextPageURL, path: String
let perPage: Int
let prevPageURL: String?
let to, total: Int

enum CodingKeys: String, CodingKey {


case data, from, path, to, total
case currentPage = "current_page"
case rstPageURL = " rst_page_url"
case lastPage = "last_page"
case lastPageURL = "last_page_url"
case nextPageURL = "next_page_url"
case perPage = "per_page"
case prevPageURL = "prev_page_url"
}
}
fi
fi
fi
public struct Datum : Hashable, Identi able, Codable {
public let id = UUID() // <-- could be Int
public let title: String
public let published: String
public let url: String

public init( title: String, published: String, url: String) {


[Link] = title
[Link] = published
[Link] = url
}

enum CodingKeys: String, CodingKey {


case title, published, url
}
}
By : Anup Kumar Mishra
fi

You might also like