I'm considering the following structure using Swift 4:
class BaseClass {
// abstract function
func doSomething<T: BaseClass>(with object: T) {}
}
class SubClass: BaseClass {
var title: String = "hello world"
// implementation
override func doSomething<T: BaseClass>(with object: T) {
// do something with object
// can I access object.title here?
}
}
In the above snippet the object.title property is not accessible, because apparently type of object property is BaseClass, not SubClass.
I assume the function parameter type should be set as Self, such as:
func doSomething(with object: Self) {}
but the following error occurs while compilation.
'Self' is only available in a protocol or as the result of a method in a class; did you mean 'BaseClass'?
Is there a way to reference type of self while overriding functions from base class?
Edit
So my question is whether we can access SubClass's properties inside functions that overrides super class's functions (as object.title property is in SubClass implementation). I was considering a whole lot of classes that inherit base class, so I thought it might be useful to have functions that interacts with another instance with the same type when called.