I have a design related question.
There is a class inherited from CAShapeLayer and one another class inherited from CATextLayer and both of them confirm to a certain protocol like below.
protocol HogeProtocol {
func aFunction()
}
class A: CAShapeLayer, HogeProtocol {
func aFunction() {
print("I am Class A")
}
}
class B: CATextLayer, HogeProtocol {
func aFunction() {
print("I am Class B")
}
}
A subclass of UIView has an array of objects that confirm this protocol:
class CustomView: UIView {
var customLayers = [HogeProtocol]()
func callTheFunctionAndAddSublayer() {
// some implementation
}
}
What I am trying to do here is to call aFunction() of the customLayers and add them to them to the layer of this custom UIView.
class CustomView: UIView {
var customLayers = [HogeProtocol]()
func callTheFunctionAndAddSublayer() {
for customLayer in customLayers {
customLayer.aFunction() // can call
layer.addSublayer(customLayer) // cannot call..
}
}
}
In this case, the elements is confirming protocol but cannot be added to sublayers because they are not inherited from CALayer. I wish I could make an array of an object inherited from CALayer (which is the common parent class for CAShapeLayer and CATextLayer) AND confirming to the protocol but swift doesn't allow me to do so (as far as I know...)
It seems very a simple problem and guess there might be a solution already but I couldn't find any answers after hours of google researching... Any ideas?