Screenshot of the NSManagedObject subclasses I have created NSManagedObject subclasses
Im trying to copy a Cycle and all the TrainingWeeks, TrainingSessions and sets it contains. This cycle copying method I have created should in theory work but the cycle it returns contains a trainingWeek which itself contains trainingWeeks as well which causes a type casting error when its trying to cast TrainingWeek to TrainingSession. I can't figure out why?
The Cycle I'm copying contains 1 TrainingWeek in its ttoContainer, the TrainingWeek contains 1 TrainingSesison in its ttoContainer, this TrainingSession contains 1 Set in its ttoContainer. All these objects subclass TrainingTimeObject and implements TTOCopyProtocol (A method that just creates a new obj of the same type and manually sets the attributes of that objects to the same of the old obj). All objects but Set implements TTOContainerProtocol (The obj has a ttoContainer).
Here is the code for the method:
private func copyTTOandContainer(obj: TrainingTimeObject) -> TrainingTimeObject? {
// cast as conforming to ttoCopyProtocol
guard let obj: TTOCopyProtocol = obj as? TTOCopyProtocol else {return nil}
// Create a copy of the obj
let objCopy = obj.ttoCopy()
// cast as tto with a container
guard let objCopy = objCopy as? TTOContainerProtocol else {return objCopy}
// get the container
guard let objCopyTTOContainer = objCopy.ttoContainer else {return objCopy as? TrainingTimeObject}
// cast the obj argument as conforming to TTOContainerProtocol
guard let objContainer: TTOContainerProtocol = obj as? TTOContainerProtocol else {return objCopy as? TrainingTimeObject}
// get container as array
let objContainerAsArray: [TrainingTimeObject] = objContainer.ttoContainer!.getTTOContainerAsArray()
// Iterate through container
for tto in objContainerAsArray {
// cast as copyable tto
guard let tto: TTOCopyProtocol = tto as? TTOCopyProtocol else {continue}
// copy the tto from the container
let ttoCopy: TrainingTimeObject = tto.ttoCopy()
// add the copy to the container of the obj copy
objCopyTTOContainer.addToTTOContainer(tto: ttoCopy)
// check if the sub tto copy contains a container, if so copy that aswell (Creates recursion loop until all obj in the data 'tree' are copied)
guard let ttoCopyWithSubTTO: TrainingTimeObject = copyTTOandContainer(obj: tto) else {continue}
// remove the old obj
objCopyTTOContainer.removeFromTTOContainer(tto: ttoCopy)
// add the new obj
objCopyTTOContainer.addToTTOContainer(tto: ttoCopyWithSubTTO)
}
return objCopy as? TrainingTimeObject
}
What I tried:
- Rewriting the method, expected this to catch any human error with the code, resulted in the same result.
- I stepped through the code expecting the method to create a TrainingWeek in the TrainingWeek, noted that it actually doesn't and that it does what it is supposed to do confusing me further on how the return becomes wrong?
- Went through all the ttoCopy() implementations and Constructors, expected the entity description to be wrong somewhere, it wasn't, its all correct.