SWIFT "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
SWIFT "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
这是我第一次提问。
我以为我做的一切都正确。我的模型建错了吗?我不确定如何修复此错误。
这是我的代码,在正确的模型之后我想将数据传递给 table:
func createJSON() {
if let url = URL(string: "https://newsapi.org/v2/everything?q=apple&from=2020-11-15&to=2020-11-15&sortBy=popularity&apiKey=c5722efe6e65432fb5c116d3e1403dca") {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, response, error) in
guard let data = data, error == nil else { return }
var result: NewsResult?
do {
result = try JSONDecoder().decode(NewsResult.self, from: data)
} catch {
print("error masage: \(error)")
}
guard let finalResult = result else { return }
print(finalResult.status)
print(finalResult.totalResults)
// print(finalResult.articles)
// let newNews = finalResult.artiscles
// self.newsApple.append(contentsOf: newNews)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
task.resume()
}
}
// MARK: - Model Data Source
struct NewsResult:Codable {
let status: String
let totalResults: Int // totalResults
let articles: [articles]
}
struct articles:Codable {
let author: String // articles[0].author
let title: String
let description: String
let url:String
let urlToImage:String
let publishedAt: String
let content: String
}
您编写的模型代码未与 JSON 结果正确映射。尝试用以下代码替换您的模型结构:
struct NewsResult: Codable {
var status: String?
var totalResults: Int?
var articles: [Article]?
}
struct Article: Codable {
var source: Source?
var author: String?
var title: String?
var articleDescription: String?
var url: String?
var urlToImage: String?
var publishedAt: String?
var content: String?
}
struct Source: Codable {
var id : String?
var name: String?
}
这是我第一次提问。 我以为我做的一切都正确。我的模型建错了吗?我不确定如何修复此错误。 这是我的代码,在正确的模型之后我想将数据传递给 table:
func createJSON() {
if let url = URL(string: "https://newsapi.org/v2/everything?q=apple&from=2020-11-15&to=2020-11-15&sortBy=popularity&apiKey=c5722efe6e65432fb5c116d3e1403dca") {
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, response, error) in
guard let data = data, error == nil else { return }
var result: NewsResult?
do {
result = try JSONDecoder().decode(NewsResult.self, from: data)
} catch {
print("error masage: \(error)")
}
guard let finalResult = result else { return }
print(finalResult.status)
print(finalResult.totalResults)
// print(finalResult.articles)
// let newNews = finalResult.artiscles
// self.newsApple.append(contentsOf: newNews)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
task.resume()
}
}
// MARK: - Model Data Source
struct NewsResult:Codable {
let status: String
let totalResults: Int // totalResults
let articles: [articles]
}
struct articles:Codable {
let author: String // articles[0].author
let title: String
let description: String
let url:String
let urlToImage:String
let publishedAt: String
let content: String
}
您编写的模型代码未与 JSON 结果正确映射。尝试用以下代码替换您的模型结构:
struct NewsResult: Codable {
var status: String?
var totalResults: Int?
var articles: [Article]?
}
struct Article: Codable {
var source: Source?
var author: String?
var title: String?
var articleDescription: String?
var url: String?
var urlToImage: String?
var publishedAt: String?
var content: String?
}
struct Source: Codable {
var id : String?
var name: String?
}