shiju86.v
shiju86.v

Reputation: 667

How to use generics in own created model class in iOS?

How to use generics in own created model class?
I have one FeatureListModel class and other have FavoriteModel class. Both store the same properties, the only difference is the different class model name.

I need to display model properties value in ProductDetail controller.

How could I manage this stuff using generics?

Here is my code (Swift 4.2):

1st Model: FavoriteListModel

class FavoriteListModel {

    var categoryID: Int?

    var item_name: String?

    var MRP: String?

}

2nd Model: FeatureListModel

class FeatureListModel {

    var categoryID: Int?

    var item_name: String?

    var MRP: String?

}

I have 8-10 more properties, but this is just some stuff in my code.

Controller - ProductDetailTableViewController

class ProductDetailTableViewController : UITableViewController {

    var productDetails: FavoriteListModel!

    var productFeatureList: FeatureListModel!

   fileprivate func displayProduct() {

      if productDetails != nil {

        title = productDetails.item_name

        categoryID = productDetails.categoryID!

       }else if productFeatureList != nil {

          categoryID = productFeatureList.categoryID!

          title = productFeatureList.item_name

  }
}

and in my Product Detail Table Controller, I am accessing model objects and display on the screen. I don't want if-else check.

Upvotes: 0

Views: 728

Answers (2)

vadian
vadian

Reputation: 285160

You are mixing up generics and protocols. In your case a protocol is preferable.

In ProductDetailTableViewController there is an object which responds to the getter of item_name (by the way please conform to the camelCased naming convention itemName) and categoryID. The type of the object as well as the existence of other properties and functions is not significant.

Create a protocol

protocol Listable {
   var itemName : String { get }
   var categoryID : Int { get }
}

Then adopt the protocol in your classes (do you really need a class?) and declare at least categoryID as non-optional since you are force unwrapping the value later anyway. Don't use optionals as an alibi not to write an initializer.

class FavoriteListModel : Listable { ...
class FeatureListModel : Listable { ...

In ProductDetailTableViewController rather than two properties declare one property as Listable and instead of objective-c-ish nil checking use optional binding:

var details: Listable!

fileprivate func displayProduct() {
   if let productDetails = details {
      title = productDetails.itemName
      categoryID = productDetails.categoryID
   }
}

Upvotes: 1

user7649191
user7649191

Reputation:

What you have here is not a use case for the Generics. Generics are used when you have for example a function that does exact same thing but can be used with two different parameter types. That's when you use generics.

Another concept is super class (parent class or base class) which is used when you have a class with common properties and then other classes with those properties and then extra and different unique properties which in this case, each class subclasses the parent class.

What you have here is neither of them. A good architecture for this case is just a single model type (class or struct) and using two different collections (array or set) in your view controller.

You can also create a favorite class or featured class which holds an array with your models.

Upvotes: 0

Related Questions