Overriding a generic function produce an error
Just remove <T>
in function. Because you have T in typealias
.
protocol BaseCellProtocol {
associatedtype T
func configure(with object: T?)
}
class BaseTableViewCell: UITableViewCell, BaseCellProtocol {
typealias T = String
func configure(with object: T?) {
}
}
class TableViewCell: BaseTableViewCell {
override func configure(with object: String?) {
label.text = object
}
}
error when having class method in a generic class with same name as non-generic super class
You cannot override the function since the parameter types are difference, hence, one function cannot act as the other. Also, you cannot use the same method signature, if it already exists. The only solution is to change the name, or use the same type for the parameter as the superclass. Also, you cannot hide methods in superclasses, but you could throw an exception if it is used by overriding it.
Swift: Method overriding in parameterized class
It has nothing at all to do with the generics (what you call "parameterized"). It has to do with how one function type is substitutable for another in Swift. The rules is that function types are contravariant with respect to their parameter types.
To see this more clearly, it will help to throw away all the misleading generic stuff and the override stuff, and instead concentrate directly on the business of substituting one function type for another:
class A {}
class B:A {}
class C:B {}
func fA (x:A) {}
func fB (x:B) {}
func fC (x:C) {}
func f(_ fparam : B -> Void) {}
let result1 = f(fB) // ok
let result2 = f(fA) // ok!
// let result3 = f(fC) // not ok
We are expected to pass to the function f
as its first parameter a function of type B -> Void
, but a function of type A -> Void
is acceptable instead, where A is superclass of B.
But a function of type C -> Void
is not acceptable, where C is subclass of B. Functions are contravariant, not covariant, on their parameter types.
Related Topics
How to Move Application's Window Between Virtual Desktops in Os X
How to Perform Face Detection in Swift
How to Test Required Init(Coder:)
Mfmailcomposeviewcontroller Error [Mc] Filtering Mail Sheet Accounts for Bundle Id
Swift 3: Converting Data to String Returns a Nil Value
How to Use Nsvisualeffectview to Blend Window with Background
Swiftui - How to Get Coordinate/Position of Clicked Button
Using Nil-Coalescing Operator with Try? for Function That Throws and Returns Optional
How to Make a Function Complete Before Calling Others in an Ibaction
Changing The Color of a Button in Swiftui on Tvos
Solving System of Equations in Swift
Sharing Highscore with Social Media
How to Conform an Observableobject to the Codable Protocols
API Violation - Multiple Calls Made to -[Xctestexpectation Fulfill]
How to Define Static Constant in a Generic Class in Swift
How Would I Create a Constant That Could Be One of Several Strings Depending on Conditions