Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 数据映射问题_Ios_Swift - Fatal编程技术网

Ios 数据映射问题

Ios 数据映射问题,ios,swift,Ios,Swift,我正在尝试将数据从API映射到自定义类,但在控制台“typeMismatch(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“预期解码数组,但找到了字典。”,underlineError:nil)中出现此错误。不确定当它在其中显示一个数组时为什么会显示此内容;如何解决此问题。错误 struct News: Codable { var status: String? var totalR

我正在尝试将数据从API映射到自定义类,但在控制台“typeMismatch(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“预期解码数组,但找到了字典。”,underlineError:nil)中出现此错误。不确定当它在其中显示一个数组时为什么会显示此内容;如何解决此问题。错误

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
    字符串附加。