I want to subclass CAShapeLayer
so it will hold a reference to the corresponding object in the data model:
class CPCoursePointLayer : CAShapeLayer
{
let itsDataRef :CPCoursePoint
let itsEventData :CPEventData
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init( coursePoint: CPCoursePoint, in eventData :CPEventData)
{
itsDataRef = coursePoint
itsEventData = eventData
}
}
Where itsDataRef
and itsEventData
are references (pointers if you like) to the underlying data. This way I can update the model if I have moved the an instance of may layer class, or draw it using graphic styles specified in the eventData
. My CPCoursePointLayer
does not own these variables, to use a Rust term.
Now that I'm required to implement a init(coder:)
initializer, how do I initialize my instance variables from nothing? Can I decode a 'pointer' from the NSCoder
? That doesn't make sense to me...
Although I'm never going to call init(coder:)
, myself, I guess it's there because the system might call it. Then I will have an instance that is unusable...
I want to avoid the extra burden of doing the extra if let
in every method if I declare my instance variables as optional (?).