I have two Core Data entities called PreviousWorkout, and ExerciseData. The PreviousWorkout entity holds the following attributes: date, workoutName, and exercises. exercises is supposed to be an array of ExerciseData. After making the class with Editor > Create NSManagedObject Subclass, I then did the same thing with the ExerciseData entity.
I'm unsure whether or not this is the correct way of storing data using Core Data. My goal is to have a PreviousWorkout which contains an array of ExerciseData as well as date and workoutName.
The way I'm creating it in code is by creating a managedbjectContext with:
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
Then I create a PreviousWorkout array with:
let newItem = NSEntityDescription.insertNewObjectForEntityForName("PreviousWorkout", inManagedObjectContext: managedObjectContext!) as PreviousWorkout
I add all my stuff (date and workoutName) to newItem, then I attempt to create an array of ExerciseData with:
var exerciseData = NSEntityDescription.insertNewObjectForEntityForName("ExerciseData", inManagedObjectContext: managedObjectContext!) as [ExerciseData]
The program crashes at this point. I realize that this last line is very wrong but I'm not sure how to create an array of ExerciseData. After creating the array I would like to append to it, and then set values for its attributes before setting the newItem.exercises equal to the ExerciseData object.
Is this the correct way of creating a Core Data object that contains an array? How do I create the array of [ExerciseData]?
Thanks in advance