【发布时间】:2019-09-11 22:21:03
【问题描述】:
我是 Swift 新手,我正在尝试为我的 woocommerce 商店创建一个移动应用程序。我创建了一个带有名称、价格和图像 src 的产品类。我还创建了另一个 productBank 来存储我的产品对象,但我不知道如何使用 Alamofire 使用 JSON 填充它。
class Simpleproduct {
var productName: String
var productPrice: Int
var ProductImage: String
init(price: Int, name: String, image: String) {
productName = name
productPrice = price
ProductImage = image
}
}
class Productbank {
var productlist = [Simpleproduct]()
init() {
productlist.append(Simpleproduct(price: 5, name: "productname", image: "imagesrc"))
productlist.append(Simpleproduct(price: 5, name: "productname", image: "imagesrc"))
productlist.append(Simpleproduct(price: 5, name: "productname", image: "imagesrc"))
productlist.append(Simpleproduct(price: 5, name: "productname", image: "imagesrc"))
// [...]
【问题讨论】:
-
您是否正在检索产品以通过需要使用 Alamofire 的端点显示,因为我在您发布的代码中没有看到它?您需要阅读
Codable协议。如果您从端点获取数据,我建议将您的类转换为符合Codable协议的struct。这将使您能够放弃初始化程序(假设您得到一个干净的 JSON,其中 Ints 不像字符串等愚蠢的东西)