我在下面的 func 中的“books = hardfictList.books”行中收到错误“类型为‘[ListB]’的值没有成员‘books’”
func fetchAPIDataTop100FHC() {
let url = myUrl
AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil)
.response{ resp in
switch resp.result{
case .success(let data):
do{
let jsonData = try JSONDecoder().decode(NYTTop100FictionModel.self, from: data!)
let hardfictList = jsonData.results.listsb.filter ({ $0.listNameEncoded == "hardcover-fiction" })
//print(jsonData.results.listsb)
books = hardfictList.books
} catch {
print(String(describing: error))
}
case .failure(let error):
print(String(describing: error))
}
} //end response
} //end fetch API
下面是结构 ListB 和 Book 的代码
struct Result: Decodable {
let bestsellersDate: String
let publishedDate: String
let publishedDateDescription: String
let previousPublishedDate: String
let nextPublishedDate: String
let listsb: [ListB]
private enum CodingKeys: String, CodingKey {
case bestsellersDate = "bestsellers_date"
case publishedDate = "published_date"
case publishedDateDescription = "published_date_description"
case previousPublishedDate = "previous_published_date"
case nextPublishedDate = "next_published_date"
case listsb = "lists"
}
init(bestsellersDate: String, publishedDate: String, publishedDateDescription: String, previousPublishedDate: String, nextPublishedDate: String, listsb: [ListB]) {
self.bestsellersDate = bestsellersDate
self.publishedDate = publishedDate
self.publishedDateDescription = publishedDateDescription
self.previousPublishedDate = previousPublishedDate
self.nextPublishedDate = nextPublishedDate
self.listsb = listsb
}
}
struct ListB: Decodable {
//struct ListB: Codable {
let listID: Int
let listName: String
let listNameEncoded: String
let displayName: String
let updated: String
//let listImage: Any?
//let listImageWidth: Any?
//let listImageHeight: Any?
let books: [Book]
private enum CodingKeys: String, CodingKey {
case listID = "list_id"
case listName = "list_name"
case listNameEncoded = "list_name_encoded"
case displayName = "display_name"
case updated
//case listImage = "list_image"
//case listImageWidth = "list_image_width"
//case listImageHeight = "list_image_height"
case books
}
init(listID: Int, listName: String, listNameEncoded: String, displayName: String, updated: String, books: [Book]) {
//init(listID: Int, listName: String, listNameEncoded: String, displayName: String, updated: String, listImage: Any?, listImageWidth: Any?, listImageHeight: Any?, books: [Book]) {
self.listID = listID
self.listName = listName
self.listNameEncoded = listNameEncoded
self.displayName = displayName
self.updated = updated
//self.listImage = listImage
//self.listImageWidth = listImageWidth
//self.listImageHeight = listImageHeight
self.books = books
}
}
struct Book: Codable, Identifiable {
let id = UUID()
let ageGroup: String
let amazonProductURL: URL
let articleChapterLink: String
let author: String
let bookImage: URL?
let bookImageWidth: Int?
let bookImageHeight: Int?
let bookReviewLink: String
let bookUri: URL
let btrn: String
let contributor: String
let contributorNote: String
let createdDate: String
let description: String
let firstChapterLink: String
let price: String
let primaryIsbn10: String
let primaryIsbn13: String
let publisher: String
let rank: Int
let rankLastWeek: Int
let sundayReviewLink: String
let title: String
let updatedDate: String
let weeksOnList: Int
let buyLinks: [BuyLink]
private enum CodingKeys: String, CodingKey {
case ageGroup = "age_group"
case amazonProductURL = "amazon_product_url"
case articleChapterLink = "article_chapter_link"
case author
case bookImage = "book_image"
case bookImageWidth = "book_image_width"
case bookImageHeight = "book_image_height"
case bookReviewLink = "book_review_link"
case bookUri = "book_uri"
case btrn
case contributor
case contributorNote = "contributor_note"
case createdDate = "created_date"
case description
case firstChapterLink = "first_chapter_link"
case price
case primaryIsbn10 = "primary_isbn10"
case primaryIsbn13 = "primary_isbn13"
case publisher
case rank
case rankLastWeek = "rank_last_week"
case sundayReviewLink = "sunday_review_link"
case title
case updatedDate = "updated_date"
case weeksOnList = "weeks_on_list"
case buyLinks = "buy_links"
}
init(ageGroup: String, amazonProductURL: URL, articleChapterLink: String, author: String, bookImage: URL?, bookImageWidth: Int?, bookImageHeight: Int?, bookReviewLink: String, bookUri: URL, btrn: String, contributor: String, contributorNote: String, createdDate: String, description: String, firstChapterLink: String, price: String, primaryIsbn10: String, primaryIsbn13: String, publisher: String, rank: Int, rankLastWeek: Int, sundayReviewLink: String, title: String, updatedDate: String, weeksOnList: Int, buyLinks: [BuyLink]) {
self.ageGroup = ageGroup
self.amazonProductURL = amazonProductURL
self.articleChapterLink = articleChapterLink
self.author = author
self.bookImage = bookImage
self.bookImageWidth = bookImageWidth
self.bookImageHeight = bookImageHeight
self.bookReviewLink = bookReviewLink
self.bookUri = bookUri
self.btrn = btrn
self.contributor = contributor
self.contributorNote = contributorNote
self.createdDate = createdDate
self.description = description
self.firstChapterLink = firstChapterLink
self.price = price
self.primaryIsbn10 = primaryIsbn10
self.primaryIsbn13 = primaryIsbn13
self.publisher = publisher
self.rank = rank
self.rankLastWeek = rankLastWeek
self.sundayReviewLink = sundayReviewLink
self.title = title
self.updatedDate = updatedDate
self.weeksOnList = weeksOnList
self.buyLinks = buyLinks
}
}
'''
filter
返回一个包含与过滤器中的条件匹配的所有元素的数组。如果您确定只有一个元素符合条件,请使用first(where:)
: