What is the proper way to copy layer references into presentation layer?

I have MyLayer and it has a sublayer that it also holds a reference to. In that setup what is the proper way to override init(layer: Any)?

Right now I'm doing it like this ... layer.mySublayer.presentation()! is that correct? Is that always going to work, or will presentation ever be nil in this context?

Thanks for any tips!

class MyLayer: CALayer {

    var mySublayer: CALayer

    public override init() {
        mySublayer = CALayer()
        super.init()
        addSublayer(mySublayer)
    }

    override init(layer: Any) {
        let layer = layer as! MyLayer
        mySublayer = layer.mySublayer.presentation()!
        super.init(layer: layer)
    }

    required init?(coder: NSCoder) {
        fatalError("has not been implemented")
    }

}

Replies

I'm seeing this is NOT a good pattern. Found a case where layer.mySublayer.presentation()! in context of init(layer: Any).

With that said I still am not quite sure what the correct behavior should be... does it make sense for the presentation layer to hold references to model layers? So for example replace the above with:

override init(layer: Any) {
        let layer = layer as! MyLayer
        mySublayer = layer.mySublayer
        super.init(layer: layer)
}