0

Is it possible to get something like below using generics in swift? Any help would be appreciated more than anything else.

class ABC {
   var title: String?
}

class Option: <T> {
   var name: String?
   var data: T?
}

let object1 = ABC()

let option = Option(name: "Test", data: object1)

// Getting data again
let data = option.data
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32

1 Answers1

1

Here is how you can use Generics in Swift.

protocol PPP {
    var title: String? {get set}
}

class ABC: PPP {
    var title: String?
}

class XYZ: PPP {
    var title: String?
}

class Option<T> {
    var name: String?
    var data: T?

    init(name: String?, data: T?) {
        self.name = name
        self.data = data
    }
}

let object1 = ABC()
let option1 = Option(name: "Test1", data: object1)

let object2 = XYZ()
let option2 = Option(name: "Test2", data: object2)

Since classes in Swift doesn't have a default initializer, so create one that accepts 2 parameters - String? and T?.

You can use type(of:) method to identify the type of an object.

print(type(of: option1.data)) //ABC
print(type(of: option2.data)) //XYZ

You can use protocol to access title in both ABC and XYZ.

Conform ABC and XYZ to protocol PPP and implement its title property in both ABC and XYZ.

print(option1.data?.title)
print(option2.data?.title)
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Thanks for the answer. But I need to identify the dataType for data in the last line. How do I do that without type casting? – Amrit Sidhu May 21 '19 at 17:06
  • This is correct which I am already aware of. I am looking for something like: option1.data.title Can I directly access the variable with out checking its type? I guess that can be done using protocols? – Amrit Sidhu May 21 '19 at 17:12
  • Updated that too. Give it a try. – PGDev May 21 '19 at 17:15
  • Thank you. That's a womnderful help. Does that mean I'll have to declare all the variables of classes ABC() and XYZ() in PPP protocol? – Amrit Sidhu May 21 '19 at 17:17
  • No. But the reverse is `true`. You need to implement mandatory `variables` and `methods` of `PPP` in `ABC` and `XYZ`. – PGDev May 21 '19 at 17:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193718/discussion-between-amrit-sidhu-and-pgdev). – Amrit Sidhu May 21 '19 at 17:19
  • A little more confusion: Now its asks to add a "Any" suffix let option1 = Option(name: "Test1", data: object1) That makes data of type any which is not letting me directly fetch the variables from data? – Amrit Sidhu May 21 '19 at 17:39
  • Add the same type in angular brackets that you are passing as data `let option1 = Option(name: "Test1", data: object1)` – PGDev May 21 '19 at 17:41
  • One question - Do you really need `class Option` now? I think your requirement is now satisfied using `protocol`. Check identify the use case. – PGDev May 21 '19 at 17:43
  • Yes, class Option is required as it holds other properties related to a tableView. – Amrit Sidhu May 21 '19 at 17:44
  • Your answer is great!! This worked. But I got another problem. I do not want to add all of the properties of XYZ() into ABC(). However PPP protocol compels to add all properties.. Is there any way I can avoid this? – Amrit Sidhu May 21 '19 at 18:00
  • I was able to make it optional. Thanks Dude!! – Amrit Sidhu May 21 '19 at 18:15
  • Hi @PGDev Can you please try to answer this question: https://stackoverflow.com/questions/56382822/uisearchbar-not-changing-search-icon-and-placeholder-color-for-ios-12-1?noredirect=1#comment99366474_56382822 – Amrit Sidhu May 30 '19 at 17:45