如何遵守Swift中的顺序协议 4
How To Conform to the Sequence Protocol in Swift 4
目前正在尝试将 json 字典解析到 swift 中的屏幕,但是我 运行 遇到了这个序列协议错误,还有一个类型不匹配错误string/data 并期望一个 Int。错误是 "Type 'ProductResponse' does not conform to protocol 'Sequence'" 和
"typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath:
[CodingKeys(stringValue: "code", intValue: nil)], debugDescription:
"Expected to decode Int but found a string/data instead.",
underlyingError: nil))"
struct ProductResponse: Codable {
let code: String
let product: Product
let statusVerbose: String
let status: Int
enum CodingKeys: String, CodingKey {
case code, product
case statusVerbose = "status_verbose"
case status
}
}
struct Product: Codable {
let code: String
let productName: String
enum CodingKeys: String, CodingKey {
case code
case productName = "product_name"
}
}
class ViewController: UIViewController {
//var products = [Product]()
let API_URL = "https://carsdata/api/v0/product/5000112630794.json"
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(API_URL).responseJSON {
response in
let json = response.data
do
{
let decoder = JSONDecoder()
let productData = try decoder.decode(ProductResponse.self, from: json!)
for product in productData{ print(product.productName!) } }
catch
let err{ print(err) }
}
}
}
问题是您没有按原样解析 JSON。你这里有两个对象。 ProductResponse
和 Product
并且您正在尝试将其解析为一个。 productName
是 Product
的一部分,但您正试图从 ProductResponse
获取它。为清楚起见,我建议您创建 2 个实体并尝试这样做:
struct ProductResponse: Codable {
let code: String
let product: Product
let statusVerbose: String
let status: Int
enum CodingKeys: String, CodingKey {
case code, product
case statusVerbose = "status_verbose"
case status
}
}
struct Product: Codable {
let code: String
let productName: String
enum CodingKeys: String, CodingKey {
case code
case productName = "product_name"
}
}
并尝试解码 ProductResponse.self
。
目前正在尝试将 json 字典解析到 swift 中的屏幕,但是我 运行 遇到了这个序列协议错误,还有一个类型不匹配错误string/data 并期望一个 Int。错误是 "Type 'ProductResponse' does not conform to protocol 'Sequence'" 和
"typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "code", intValue: nil)], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil))"
struct ProductResponse: Codable {
let code: String
let product: Product
let statusVerbose: String
let status: Int
enum CodingKeys: String, CodingKey {
case code, product
case statusVerbose = "status_verbose"
case status
}
}
struct Product: Codable {
let code: String
let productName: String
enum CodingKeys: String, CodingKey {
case code
case productName = "product_name"
}
}
class ViewController: UIViewController {
//var products = [Product]()
let API_URL = "https://carsdata/api/v0/product/5000112630794.json"
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(API_URL).responseJSON {
response in
let json = response.data
do
{
let decoder = JSONDecoder()
let productData = try decoder.decode(ProductResponse.self, from: json!)
for product in productData{ print(product.productName!) } }
catch
let err{ print(err) }
}
}
}
问题是您没有按原样解析 JSON。你这里有两个对象。 ProductResponse
和 Product
并且您正在尝试将其解析为一个。 productName
是 Product
的一部分,但您正试图从 ProductResponse
获取它。为清楚起见,我建议您创建 2 个实体并尝试这样做:
struct ProductResponse: Codable {
let code: String
let product: Product
let statusVerbose: String
let status: Int
enum CodingKeys: String, CodingKey {
case code, product
case statusVerbose = "status_verbose"
case status
}
}
struct Product: Codable {
let code: String
let productName: String
enum CodingKeys: String, CodingKey {
case code
case productName = "product_name"
}
}
并尝试解码 ProductResponse.self
。