denis631 denis631 - 1 year ago 45
Swift Question

Generic of Generics ? class A<T<U>>?

Can I somehow force the generic type to have a generic type ?
I want to have some functions, that have as a parameter type U, so how can I do that ?


class TableViewViewModel<T<U>> {
typealias SectionDataType = T
typealias RowDataType = U

var sections = [SectionDataType<RowDataType>]()

Answer Source

Try declaring a protocol SectionDataType that requires an associated type. This means that any conforming type (like Section below) must implement a typealias. Then, in TableViewViewModel you can access the type which you were calling U through that typealias.

protocol SectionDataType {
    associatedtype RowDataType

struct Section<U>: SectionDataType {
    typealias RowDataType = U

class TableViewViewModel<T: SectionDataType> {
    typealias RowDataType = T.RowDataType

    var sections = [T]()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download