ios - iOS-选择模型类别

标签 ios objective-c swift

我可以使用相同的模型类来存储具有相同键的字典的多类型数组的数组数据吗?

比方说,例如,我有一个名为的模型类ProductDetail 用于存储具有键ID,名称和图像的产品详细信息,并将其显示在UITableViewController中。

现在,我有一个不同的类,名为category,具有与上述相同的键。

这是我的模型课:

class TrendingProductsData: NSObject {

    var id : Int! = 0
    var name : String! = ""
    var image : String! = ""

}

我的问题是我也可以使用 ProductDetail 模型来存储类别数据吗?

最佳答案

如何将 super 模型用于常见属性并扩展您拥有的属性。这是我的意思:


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
    }
}


这样,您可以创建具有共同属性的ProductDetailCategories模型,如果需要,可以添加单独的属性和函数。

关于ios - iOS-选择模型类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60538384/

相关文章:

ios - 如何在其他 ViewController 中继续下载数据

ios - UINavigationBar 中的 UILabel 在横向中被切断

ios - UITableView 重用不同部分的内容

iphone - 当 iPhone 在 iOS5 上被锁定时,如何防止应用程序被发送终止信号?

ios - UISplitviewcontroller - 主视图和详细 View 中的大小类问题

json - JSON/NSJSONSerialization 的 Swift 问题

ios - 使用生物识别技术对用户进行身份验证导致应用程序崩溃

ios - 核心数据插入和保存缓慢

iphone - WhatsApp 如何知道我联系人的国家代码?

swift - 从 ViewController 中的登录到 UITabBarController 的转换