Ios 数据映射问题
我正在尝试将数据从API映射到自定义类,但在控制台“typeMismatch(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“预期解码数组,但找到了字典。”,underlineError:nil)中出现此错误。不确定当它在其中显示一个数组时为什么会显示此内容;如何解决此问题。错误Ios 数据映射问题,ios,swift,Ios,Swift,我正在尝试将数据从API映射到自定义类,但在控制台“typeMismatch(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“预期解码数组,但找到了字典。”,underlineError:nil)中出现此错误。不确定当它在其中显示一个数组时为什么会显示此内容;如何解决此问题。错误 struct News: Codable { var status: String? var totalR
struct News: Codable {
var status: String?
var totalResults: Int?
var articles: [Article]?
enum CodingKeys: String, CodingKey {
case status = "status"
case totalResults = "totalResults"
case articles = "articles"
}
}
// MARK: - 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?
enum CodingKeys: String, CodingKey {
case source = "source"
case author = "author"
case title = "title"
case articleDescription = "description"
case url = "url"
case urlToImage = "urlToImage"
case publishedAt = "publishedAt"
case content = "content"
}
}
func getAllCountryNameFrom() {
AF.request(self.baseURL + self.apiKeyPathCompononent, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil).response { (responseData) in
guard let data = responseData.data else {return}
do {
let news = try self.jsonDecoder.decode(News.self, from: data)
print(news)
} catch {
print(error)
}
}
}
将
数据作为.utf8
字符串附加。