So this is a little tricky, but I'll outline the main points:
- I have a class defined in an Objective-C header from a pre-compiled framework. We'll call
BaseClass
- This class is meant to be subclassed
BaseClass
has a class function calledfetchAll()
, which can be called by a subclass to return an array of all instances of that subclass.
For example, SubClass.fetchAll()
will return a Set<SubClass>
Now I want to write a new class function, called fetchAllNames()
Ideally, it would look like this:
extension BaseClass {
class func fetchAllNoBilly() -> Set<Self> {
return Self.fetchAll().filter{ return $0.name != "Billy" }
}
}
then for one of my subclasses ,I could just say
Subclass.fetchAllNames()
or SubClass3.fetchAllNames()
However, I get this compiler error:
'Self' is only available in a protocol or as the result of a method in a class; did you mean 'BaseClass'?
Is what I'm trying to do here possible? maybe it's just a bad pattern I shouldn't be doing, but right now we have a separate fetchAllNames()
class func for every single subclass, that looks like this:
@objc(Subclass) final class Subclass: BaseClass {
class func fetchAllNoBilly() -> Set<Self> {
return Self.fetchAll().filter{ return $0.name != "Billy" }
}
}
Which is just annoying to have that same class func in all ~20 of our subclasses
On top of that, there are other areas where we can reduce repeat code if I can figure out how to implement this pattern