Search code examples
swiftinheritancegeneric-programming

How to inheritance superClass when the superclass is a generic class in Swift


I have two questions need to be solved .

the first Question :

A Superclass , the name is FatherClass , a subclass inheritance the super class , please read this code :

class FatherClass <FT> {
}

class SubClass : FatherClass { // Error : Reference to generic type 'MyFatherClass' requires arguments in <...>
}

How to solve it ? please help me , thanks .

enter image description here

---------------------------

the second Question:

please read this code :

class FatherClass <FT>{
    FT fatheType ; // Error : Expected declaration
}

FT is a generic type ,

Why the property fatherType have error ? what's Expected declaration ?


Solution

  • You can use this

    class SubClass<FT> : FatherClass<FT> { 
    }
    
    or a specific type of FatherClass
    
    class SubClass : FatherClass<String> {
    }
    
    or 
    
    class SubClass<ST> : FatherClass<Int> {
    }