Reputation: 11
The code below shows the code I am using to fetch data from Contentful API to my SwiftUI app. However, by calling the function client.fetchArray
, I got an error message saying that the type of expression is ambiguous. I tried look up the documentation and the example code for client.fetchArray
uses the exact parameters and hence I assume the error message should be gone? Can anyone please help me out here! I am super confused! Thank you!
import SwiftUI
import Contentful
import Combine
let client = Client(spaceId: "qqq2yu4vbtpl", environmentId: "master", accessToken: "LurTiklxcr0ARTpI9z7GVTz-mjMO6OFVsoSZ8V7GJ4c")
//let client = Client(spaceId: "0ge8xzmnbp2c", accessToken: "03010b0d79abcb655ca3fda453f2f493b5472e0aaa53664bc7dea5ef4fce2676")
func getArray(id: String, completion: @escaping([Entry]) -> ()) {
let query = Query.where(contentTypeId: id)
client.fetchArray(of: Entry.self, matching: query) { result in
switch result {
case .success(let array):
DispatchQueue.main.async {
completion(array.items)
}
case .error(let error):
print(error)
}
print(result)
}
}
class CourseStore: ObservableObject {
@Published var courses: [Course] = courseData
init() {
let colors = [#colorLiteral(red: 0.3635728061, green: 0, blue: 0.8927152753, alpha: 0.8470588235), #colorLiteral(red: 0.2461163998, green: 0.3063159585, blue: 1, alpha: 0.8470588235), #colorLiteral(red: 0.3414418995, green: 0.653152287, blue: 0.405282855, alpha: 0.8470588235), #colorLiteral(red: 0.8906980753, green: 0.684363246, blue: 0, alpha: 0.8470588235), #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1), #colorLiteral(red: 0.5058823824, green: 0.3372549117, blue: 0.06666667014, alpha: 1), #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1), #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1), #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)]
getArray(id: "course") { (items) in
items.forEach { (item) in
self.courses.append(Course(
title: item.fields["title"] as! String,
subtitle: item.fields["subtitle"] as! String,
image: item.fields["image"] as! String,
logo: item.fields["logo"] as! String,
color: colors.randomElement()!,
show: false))
}
}
}
}
For my Data.swift file, I have written the following code fyi:
import SwiftUI
struct Post: Codable, Identifiable {
var id = UUID()
var title : String
var body : String
}
class API {
func getPosts(completion: @escaping ([Post]) -> ()) {
guard let url = URL(string: "https://jsonplaceholder.typicode.com/posts") else { return }
URLSession.shared.dataTask(with: url) { (data, _, _) in
guard let data = data else { return }
let posts = try! JSONDecoder().decode([Post].self, from: data)
// Must be async to run API call
DispatchQueue.main.async {
completion(posts)
}
}
.resume()
}
}
And here is the code for my DataStore.swift, think it will be helpful for my data fetching script:
import SwiftUI
import Combine
class DataStore: ObservableObject {
@Published var posts: [Post] = []
init() {
getPosts()
}
func getPosts() {
API().getPosts { (posts) in
self.posts = posts
}
}
}
Upvotes: 1
Views: 93