I've been trying to develop an app which fetches certain information of a book from Google Books API (name, author and image) and display the image inside a collection view
var bookInfo = [Book]()
let urlString = "https://www.googleapis.com/books/v1/volumes/J8ahqXjUhAAC"
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
self.bookInfo = try JSONDecoder().decode([Book].self, from: data!)
for info in self.bookInfo {
print(info.bookName)
}
}
catch {
print("error")
}
}
}
Below is a struct which represents the contents I'm trying to capture but not sure if this is entirely correct.
struct Book: Codable {
var bookName: String
var imageURL: String
var pageCount: Int
var author: String
var format: String
}
struct volumeInfo: Codable {
var bookInfo: [Book]
}
When running this code, I don't get any errors but the app is unable to fetch anything and my UIImageView
is empty.