我可以使用相同的模型类来存储具有相同键的字典的多类型数组的数组数据吗?
例如,我有一个名为ProductDetail的模型类,该模型类用于存储具有键ID,名称和图像的产品详细信息,并将其显示在UITableViewController中。
现在,我有一个不同的类,名为category,具有与上述相同的键。
这是我的模特班:
class TrendingProductsData: NSObject {
var id : Int! = 0
var name : String! = ""
var image : String! = ""
}
我的问题是我也可以使用ProductDetail模型来存储类别数据吗?
如何将超级模型用于常见属性并扩展您拥有的属性。这是我的意思:
class BaseModel: NSObject {
var id : Int = 0
var name : String = ""
var image : String = ""
func setData(data: Any) {
// Parse id, name and image from data
}
}
class ProductDetail: BaseModel {
// Add your other properties and/or functions
var productProvider: String = "" // I added this to be an example
override func setData(data: Any) {
super.setData(data: data) // Since the key-value pairs are the same id, name and image will be parsed at BaseModel
// Parse extra values such as productProvider
}
}
class Categories: BaseModel {
// Add your other properties and/or functions
var categorySubtitle: String = "" // I added this to be an example
override func setData(data: Any) {
super.setData(data: data) // Since the key-value pairs are the same id, name and image will be parsed at BaseModel
// Parse extra values such as categorySubtitle
}
}
这样,您可以创建具有共同属性的ProductDetail
和Categories
模型,如果需要,可以添加单独的属性和函数。