简体   繁体   中英

Manage single variable for two different types of data

I have two UI of listing, the cells are the same in both listing. For both UI, the different Codable array is managed. ie FirstViewController contains FirstDataModel & SecondViewController contains SecondDataModel .

So in FirstViewController , in cellForRow method I have called the code :

func setPastCell(cell:WeekCell, data_dic:FirstDataModel) {
    cell.lblGoalA.text = predictScore1
    cell.lblGoalB.text = predictScore2
}

Arrays:

struct FirstDataModel: Codable {
    var team1_name:String?
    var team2_name:String?
    var status:Int?
    var image:String?
    var score:Int?

}
struct SecondDataModel: Codable {
    var team1_name:String?
    var team2_name:String?
    var status:Int?
    var image:String?
    var count:Int?
    var balance:Int?
}

I want to make common function for both FirstDataModel & SecondDataModel . So how can I manage it using generic function?

How can I pass FirstDataModel or SecondDataModel in setPastCell(cell: WeekCell, data_dic: FirstDataModel) ?

Thanks in advance!

You can inherit the two DataModel from a Protocol, and use Protocol properties in setup.

Example:

protocol ModelProtocol {
    var descriptionOne: String { get }
    var descriptionTwo: String { get }
}

struct FirstDataModel: ModelProtocol {}

func setPastCell(cell:WeekCell, data_dic: ModelProtocol) {
    cell.lblGoalA.text = descriptionOne
    cell.lblGoalB.text = descriptionTwo
}

Craete a protocol with common properties

protocol DataModel {
    var team1_name:String? { get }
    var team2_name:String? { get }
    var status:Int? { get }
    var image:String? { get }

}

Confirm to the protocol in your structs and add its properties

struct FirstDataModel: DataModel, Codable {
    //Protocol properties
    var team1_name:String?
    var team2_name:String?
    var status:Int?
    var image:String?
    //additional properties
    var score:Int?
}
struct SecondDataModel: DataModel, Codable {
    //Protocol properties
    var team1_name:String?
    var team2_name:String?
    var status:Int?
    var image:String?
    //additional properties
    var count:Int?
    var balance:Int?
}

As both structs confirm to the DataModel protocol, you can use it in the function parameter.

func setPastCell<T: DataModel>(cell:WeekCell, data_dic:T) {

    if let firstModel = data_dic as? FirstDataModel {
        print(firstModel.team1_name)
        print(firstModel.team2_name)
        print(firstModel.status)
        print(firstModel.image)
        print(firstModel.score)
    } else if let secondModel = data_dic as? SecondDataModel {
        print(secondModel.team1_name)
        print(secondModel.team2_name)
        print(secondModel.status)
        print(secondModel.image)
        print(secondModel.count)
        print(secondModel.balance)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM