I have an issue here... I am using WooCommerce API to get data from database... Everything is great with this code but i have a weird issue with getting Featured photo (featured_src), The featured photo Value is a String when the image of product exists, but when ever there is no image for the product, i get a bool value instead of a string( i get a false). And the app crashes. As you see in my code i specify the properties as String or int or.... and i set featured_src as string but sometimes i get a bool value for it. How should i edit my code?
import UIKit
struct Products: Decodable {
let products: [product]
}
struct product: Decodable {
let title: String
let id: Int
let price: String
let sale_price: String?
let featured_src: String?
let short_description: String
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let jsonUrlString = "https://www.komeil24.com/wc-api/v3/products"
guard let url = URL(string: jsonUrlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {return}
do {
let products = try JSONDecoder().decode(Products.self, from: data)
print(products.products)
} catch let jsonErr {
print("Error" , jsonErr)
}
}.resume()
}
}