Quiz app with Practice and Exam modes












2














I am building a Quiz app in swift (my first app) to practice a few skills (using CoreData, working with plists, UIKit etc.) and later to work with a server which stores the Exercises.



Here's my current point in development: https://bitbucket.org/paescebu/staplerch/src/master/



My question is not precise, more general, I hope this is accepted here. I wanted to know if what I am practicing in my Code is good or bad practice so far.
Any specifics like:
- Design pattern
- Conventions
- bad practices
are welcome



example of my uncertainties (my AppDelegate):



import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
preloadData()
return true
}

private func preloadData() {

let preloadedDataKey = "didPreloadData"
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: preloadedDataKey) == false
{
var categories: [Category] =
if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist") {
categories = readDemoDataPlist(withPath: path)
}
//preload into Core Data
let backgroundContext = persistentContainer.newBackgroundContext()
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
backgroundContext.perform
{
do {
for item in categories {
let category = CDCategory(context: backgroundContext)
category.title = item.title
category.imageName = item.imageName
if let exerciseArray = item.exercises {
for eachExercise in exerciseArray {
let exercise = CDExercise(context: backgroundContext)
exercise.question = eachExercise.question
exercise.explanation = eachExercise.explanation
exercise.imageName = eachExercise.imageName
exercise.correctAnswer = eachExercise.correctAnswer
if let answersArray = eachExercise.answers
{
for eachAnswer in answersArray {
let answer = CDAnswers(context: backgroundContext)
answer.answer = eachAnswer
exercise.addToAnswers(answer)
}
}
category.addToExercises(exercise)
}
}
}
try backgroundContext.save()
userDefaults.set(true, forKey: preloadedDataKey)
print("Sucess!")
}
catch {
print("failed saving Context:(error.localizedDescription)")
}

}
}
}

func readDemoDataPlist(withPath: String) -> [Category] {
var categoriesArray : [Category] =
if let arrayWithCategories = NSArray(contentsOfFile: withPath) as? [[String : Any]] {
for eachCategory in arrayWithCategories {
let category = Category()
if let categoryTitle = eachCategory["title"] as? String {
category.title = categoryTitle
}
if let categoryImage = eachCategory["imageName"] as? String {
category.imageName = categoryImage
}
if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]] {
var exerciseArray: [Exercise] =
for eachExercise in arrayWithExercises {
let exercise = Exercise()
if let question = eachExercise["question"] as? String {
exercise.question = question
}
if let correctAnswerIndex = eachExercise["correctAnswer"] as? String {
exercise.correctAnswer = correctAnswerIndex
}
if let answers = eachExercise["answers"] as? [String] {
exercise.answers = answers
}
if let image = eachExercise["image"] as? String {
exercise.imageName = image
}
if let explanation = eachExercise["explanation"] as? String {
exercise.explanation = explanation
}
exerciseArray.append(exercise)
category.exercises = exerciseArray
}
}
categoriesArray.append(category)
}
}
return categoriesArray
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "StaplerCH")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error (error), (error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support

func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error (nserror), (nserror.userInfo)")
}
}
}
}


UPDATE



I changed it quite a bit now, directly using the NSManagedObject model created from CoreData, instead of this step in between storing from plist, and the reading into persistent store.



Looks now like this:



import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
preloadData()
return true
}

private func preloadData() {

let preloadedDataKey = "didPreloadData"
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: preloadedDataKey) == false
{
if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist")
{
if let arrayWithCategories = NSArray(contentsOfFile: path) as? [[String : Any]]
{
//preload into Core Data
let backgroundContext = persistentContainer.newBackgroundContext()
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
backgroundContext.perform {
do {
for eachCategory in arrayWithCategories {
let category = Category(context: backgroundContext)
if let categoryTitle = eachCategory["title"] as? String {
category.title = categoryTitle
}
if let categoryImage = eachCategory["imageName"] as? String {
category.imageName = categoryImage
}
if let exerciseArray = eachCategory["exercises"] as? [[String: Any]] {
for eachExercise in exerciseArray {
let exercise = Exercise(context: backgroundContext)
if let question = eachExercise["question"] as? String {
exercise.question = question
}
if let image = eachExercise["image"] as? String {
exercise.imageName = image
}
if let explanation = eachExercise["explanation"] as? String {
exercise.explanation = explanation
}
if let arrayWithAnswers = eachExercise["answers"] as? [[String : Any]] {
for eachAnswer in arrayWithAnswers {
if let answerText = eachAnswer["text"] as? String, let answerIsCorrect = eachAnswer["isCorrect"] as? Bool {
let answer = Answer(context: backgroundContext)
answer.text = answerText
answer.isCorrect = answerIsCorrect
exercise.addToAnswers(answer)
}
}
}
category.addToExercises(exercise)
}
}
}
try backgroundContext.save()
userDefaults.set(true, forKey: preloadedDataKey)
print("Sucess!")
}
catch {
print("failed saving Context:(error.localizedDescription)")
}

}
}
}
}
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "StaplerCH")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error (error), (error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support

func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error (nserror), (nserror.userInfo)")
}
}
}
}


Short workflow: After first installation, the app will load some demoQuestions from a plist into the persistent Store using the CoreData stack, so that the app is already preloaded with some Exercises, as said, in a later stage I'd like to be able to keep Exercises on a server from which the user can keep the Exercises updated.



The App has 2 Modes, a Practice and an Exam Mode (depending of the mode the ExerciseVC behaves differently), I really tried hard to have one VC only for that.



The other two Pages of the Start Page are quite irrelevant so far.



I am very thankful for any input.










share|improve this question









New contributor




paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2














    I am building a Quiz app in swift (my first app) to practice a few skills (using CoreData, working with plists, UIKit etc.) and later to work with a server which stores the Exercises.



    Here's my current point in development: https://bitbucket.org/paescebu/staplerch/src/master/



    My question is not precise, more general, I hope this is accepted here. I wanted to know if what I am practicing in my Code is good or bad practice so far.
    Any specifics like:
    - Design pattern
    - Conventions
    - bad practices
    are welcome



    example of my uncertainties (my AppDelegate):



    import UIKit
    import CoreData

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    preloadData()
    return true
    }

    private func preloadData() {

    let preloadedDataKey = "didPreloadData"
    let userDefaults = UserDefaults.standard
    if userDefaults.bool(forKey: preloadedDataKey) == false
    {
    var categories: [Category] =
    if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist") {
    categories = readDemoDataPlist(withPath: path)
    }
    //preload into Core Data
    let backgroundContext = persistentContainer.newBackgroundContext()
    persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
    backgroundContext.perform
    {
    do {
    for item in categories {
    let category = CDCategory(context: backgroundContext)
    category.title = item.title
    category.imageName = item.imageName
    if let exerciseArray = item.exercises {
    for eachExercise in exerciseArray {
    let exercise = CDExercise(context: backgroundContext)
    exercise.question = eachExercise.question
    exercise.explanation = eachExercise.explanation
    exercise.imageName = eachExercise.imageName
    exercise.correctAnswer = eachExercise.correctAnswer
    if let answersArray = eachExercise.answers
    {
    for eachAnswer in answersArray {
    let answer = CDAnswers(context: backgroundContext)
    answer.answer = eachAnswer
    exercise.addToAnswers(answer)
    }
    }
    category.addToExercises(exercise)
    }
    }
    }
    try backgroundContext.save()
    userDefaults.set(true, forKey: preloadedDataKey)
    print("Sucess!")
    }
    catch {
    print("failed saving Context:(error.localizedDescription)")
    }

    }
    }
    }

    func readDemoDataPlist(withPath: String) -> [Category] {
    var categoriesArray : [Category] =
    if let arrayWithCategories = NSArray(contentsOfFile: withPath) as? [[String : Any]] {
    for eachCategory in arrayWithCategories {
    let category = Category()
    if let categoryTitle = eachCategory["title"] as? String {
    category.title = categoryTitle
    }
    if let categoryImage = eachCategory["imageName"] as? String {
    category.imageName = categoryImage
    }
    if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]] {
    var exerciseArray: [Exercise] =
    for eachExercise in arrayWithExercises {
    let exercise = Exercise()
    if let question = eachExercise["question"] as? String {
    exercise.question = question
    }
    if let correctAnswerIndex = eachExercise["correctAnswer"] as? String {
    exercise.correctAnswer = correctAnswerIndex
    }
    if let answers = eachExercise["answers"] as? [String] {
    exercise.answers = answers
    }
    if let image = eachExercise["image"] as? String {
    exercise.imageName = image
    }
    if let explanation = eachExercise["explanation"] as? String {
    exercise.explanation = explanation
    }
    exerciseArray.append(exercise)
    category.exercises = exerciseArray
    }
    }
    categoriesArray.append(category)
    }
    }
    return categoriesArray
    }

    func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    self.saveContext()
    }

    // MARK: - Core Data stack

    lazy var persistentContainer: NSPersistentContainer = {
    /*
    The persistent container for the application. This implementation
    creates and returns a container, having loaded the store for the
    application to it. This property is optional since there are legitimate
    error conditions that could cause the creation of the store to fail.
    */
    let container = NSPersistentContainer(name: "StaplerCH")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
    if let error = error as NSError? {
    // Replace this implementation with code to handle the error appropriately.
    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

    /*
    Typical reasons for an error here include:
    * The parent directory does not exist, cannot be created, or disallows writing.
    * The persistent store is not accessible, due to permissions or data protection when the device is locked.
    * The device is out of space.
    * The store could not be migrated to the current model version.
    Check the error message to determine what the actual problem was.
    */
    fatalError("Unresolved error (error), (error.userInfo)")
    }
    })
    return container
    }()

    // MARK: - Core Data Saving support

    func saveContext () {
    let context = persistentContainer.viewContext
    if context.hasChanges {
    do {
    try context.save()
    } catch {
    // Replace this implementation with code to handle the error appropriately.
    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    let nserror = error as NSError
    fatalError("Unresolved error (nserror), (nserror.userInfo)")
    }
    }
    }
    }


    UPDATE



    I changed it quite a bit now, directly using the NSManagedObject model created from CoreData, instead of this step in between storing from plist, and the reading into persistent store.



    Looks now like this:



    import UIKit
    import CoreData

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    preloadData()
    return true
    }

    private func preloadData() {

    let preloadedDataKey = "didPreloadData"
    let userDefaults = UserDefaults.standard
    if userDefaults.bool(forKey: preloadedDataKey) == false
    {
    if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist")
    {
    if let arrayWithCategories = NSArray(contentsOfFile: path) as? [[String : Any]]
    {
    //preload into Core Data
    let backgroundContext = persistentContainer.newBackgroundContext()
    persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
    backgroundContext.perform {
    do {
    for eachCategory in arrayWithCategories {
    let category = Category(context: backgroundContext)
    if let categoryTitle = eachCategory["title"] as? String {
    category.title = categoryTitle
    }
    if let categoryImage = eachCategory["imageName"] as? String {
    category.imageName = categoryImage
    }
    if let exerciseArray = eachCategory["exercises"] as? [[String: Any]] {
    for eachExercise in exerciseArray {
    let exercise = Exercise(context: backgroundContext)
    if let question = eachExercise["question"] as? String {
    exercise.question = question
    }
    if let image = eachExercise["image"] as? String {
    exercise.imageName = image
    }
    if let explanation = eachExercise["explanation"] as? String {
    exercise.explanation = explanation
    }
    if let arrayWithAnswers = eachExercise["answers"] as? [[String : Any]] {
    for eachAnswer in arrayWithAnswers {
    if let answerText = eachAnswer["text"] as? String, let answerIsCorrect = eachAnswer["isCorrect"] as? Bool {
    let answer = Answer(context: backgroundContext)
    answer.text = answerText
    answer.isCorrect = answerIsCorrect
    exercise.addToAnswers(answer)
    }
    }
    }
    category.addToExercises(exercise)
    }
    }
    }
    try backgroundContext.save()
    userDefaults.set(true, forKey: preloadedDataKey)
    print("Sucess!")
    }
    catch {
    print("failed saving Context:(error.localizedDescription)")
    }

    }
    }
    }
    }
    }

    func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    self.saveContext()
    }

    // MARK: - Core Data stack

    lazy var persistentContainer: NSPersistentContainer = {
    /*
    The persistent container for the application. This implementation
    creates and returns a container, having loaded the store for the
    application to it. This property is optional since there are legitimate
    error conditions that could cause the creation of the store to fail.
    */
    let container = NSPersistentContainer(name: "StaplerCH")
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
    if let error = error as NSError? {
    // Replace this implementation with code to handle the error appropriately.
    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

    /*
    Typical reasons for an error here include:
    * The parent directory does not exist, cannot be created, or disallows writing.
    * The persistent store is not accessible, due to permissions or data protection when the device is locked.
    * The device is out of space.
    * The store could not be migrated to the current model version.
    Check the error message to determine what the actual problem was.
    */
    fatalError("Unresolved error (error), (error.userInfo)")
    }
    })
    return container
    }()

    // MARK: - Core Data Saving support

    func saveContext () {
    let context = persistentContainer.viewContext
    if context.hasChanges {
    do {
    try context.save()
    } catch {
    // Replace this implementation with code to handle the error appropriately.
    // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    let nserror = error as NSError
    fatalError("Unresolved error (nserror), (nserror.userInfo)")
    }
    }
    }
    }


    Short workflow: After first installation, the app will load some demoQuestions from a plist into the persistent Store using the CoreData stack, so that the app is already preloaded with some Exercises, as said, in a later stage I'd like to be able to keep Exercises on a server from which the user can keep the Exercises updated.



    The App has 2 Modes, a Practice and an Exam Mode (depending of the mode the ExerciseVC behaves differently), I really tried hard to have one VC only for that.



    The other two Pages of the Start Page are quite irrelevant so far.



    I am very thankful for any input.










    share|improve this question









    New contributor




    paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2












      2








      2







      I am building a Quiz app in swift (my first app) to practice a few skills (using CoreData, working with plists, UIKit etc.) and later to work with a server which stores the Exercises.



      Here's my current point in development: https://bitbucket.org/paescebu/staplerch/src/master/



      My question is not precise, more general, I hope this is accepted here. I wanted to know if what I am practicing in my Code is good or bad practice so far.
      Any specifics like:
      - Design pattern
      - Conventions
      - bad practices
      are welcome



      example of my uncertainties (my AppDelegate):



      import UIKit
      import CoreData

      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {

      var window: UIWindow?

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Override point for customization after application launch.
      preloadData()
      return true
      }

      private func preloadData() {

      let preloadedDataKey = "didPreloadData"
      let userDefaults = UserDefaults.standard
      if userDefaults.bool(forKey: preloadedDataKey) == false
      {
      var categories: [Category] =
      if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist") {
      categories = readDemoDataPlist(withPath: path)
      }
      //preload into Core Data
      let backgroundContext = persistentContainer.newBackgroundContext()
      persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
      backgroundContext.perform
      {
      do {
      for item in categories {
      let category = CDCategory(context: backgroundContext)
      category.title = item.title
      category.imageName = item.imageName
      if let exerciseArray = item.exercises {
      for eachExercise in exerciseArray {
      let exercise = CDExercise(context: backgroundContext)
      exercise.question = eachExercise.question
      exercise.explanation = eachExercise.explanation
      exercise.imageName = eachExercise.imageName
      exercise.correctAnswer = eachExercise.correctAnswer
      if let answersArray = eachExercise.answers
      {
      for eachAnswer in answersArray {
      let answer = CDAnswers(context: backgroundContext)
      answer.answer = eachAnswer
      exercise.addToAnswers(answer)
      }
      }
      category.addToExercises(exercise)
      }
      }
      }
      try backgroundContext.save()
      userDefaults.set(true, forKey: preloadedDataKey)
      print("Sucess!")
      }
      catch {
      print("failed saving Context:(error.localizedDescription)")
      }

      }
      }
      }

      func readDemoDataPlist(withPath: String) -> [Category] {
      var categoriesArray : [Category] =
      if let arrayWithCategories = NSArray(contentsOfFile: withPath) as? [[String : Any]] {
      for eachCategory in arrayWithCategories {
      let category = Category()
      if let categoryTitle = eachCategory["title"] as? String {
      category.title = categoryTitle
      }
      if let categoryImage = eachCategory["imageName"] as? String {
      category.imageName = categoryImage
      }
      if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]] {
      var exerciseArray: [Exercise] =
      for eachExercise in arrayWithExercises {
      let exercise = Exercise()
      if let question = eachExercise["question"] as? String {
      exercise.question = question
      }
      if let correctAnswerIndex = eachExercise["correctAnswer"] as? String {
      exercise.correctAnswer = correctAnswerIndex
      }
      if let answers = eachExercise["answers"] as? [String] {
      exercise.answers = answers
      }
      if let image = eachExercise["image"] as? String {
      exercise.imageName = image
      }
      if let explanation = eachExercise["explanation"] as? String {
      exercise.explanation = explanation
      }
      exerciseArray.append(exercise)
      category.exercises = exerciseArray
      }
      }
      categoriesArray.append(category)
      }
      }
      return categoriesArray
      }

      func applicationWillResignActive(_ application: UIApplication) {
      // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
      }

      func applicationDidEnterBackground(_ application: UIApplication) {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      }

      func applicationWillEnterForeground(_ application: UIApplication) {
      // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
      }

      func applicationDidBecomeActive(_ application: UIApplication) {
      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
      }

      func applicationWillTerminate(_ application: UIApplication) {
      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
      // Saves changes in the application's managed object context before the application terminates.
      self.saveContext()
      }

      // MARK: - Core Data stack

      lazy var persistentContainer: NSPersistentContainer = {
      /*
      The persistent container for the application. This implementation
      creates and returns a container, having loaded the store for the
      application to it. This property is optional since there are legitimate
      error conditions that could cause the creation of the store to fail.
      */
      let container = NSPersistentContainer(name: "StaplerCH")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
      if let error = error as NSError? {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

      /*
      Typical reasons for an error here include:
      * The parent directory does not exist, cannot be created, or disallows writing.
      * The persistent store is not accessible, due to permissions or data protection when the device is locked.
      * The device is out of space.
      * The store could not be migrated to the current model version.
      Check the error message to determine what the actual problem was.
      */
      fatalError("Unresolved error (error), (error.userInfo)")
      }
      })
      return container
      }()

      // MARK: - Core Data Saving support

      func saveContext () {
      let context = persistentContainer.viewContext
      if context.hasChanges {
      do {
      try context.save()
      } catch {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
      let nserror = error as NSError
      fatalError("Unresolved error (nserror), (nserror.userInfo)")
      }
      }
      }
      }


      UPDATE



      I changed it quite a bit now, directly using the NSManagedObject model created from CoreData, instead of this step in between storing from plist, and the reading into persistent store.



      Looks now like this:



      import UIKit
      import CoreData

      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {
      var window: UIWindow?

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Override point for customization after application launch.
      preloadData()
      return true
      }

      private func preloadData() {

      let preloadedDataKey = "didPreloadData"
      let userDefaults = UserDefaults.standard
      if userDefaults.bool(forKey: preloadedDataKey) == false
      {
      if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist")
      {
      if let arrayWithCategories = NSArray(contentsOfFile: path) as? [[String : Any]]
      {
      //preload into Core Data
      let backgroundContext = persistentContainer.newBackgroundContext()
      persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
      backgroundContext.perform {
      do {
      for eachCategory in arrayWithCategories {
      let category = Category(context: backgroundContext)
      if let categoryTitle = eachCategory["title"] as? String {
      category.title = categoryTitle
      }
      if let categoryImage = eachCategory["imageName"] as? String {
      category.imageName = categoryImage
      }
      if let exerciseArray = eachCategory["exercises"] as? [[String: Any]] {
      for eachExercise in exerciseArray {
      let exercise = Exercise(context: backgroundContext)
      if let question = eachExercise["question"] as? String {
      exercise.question = question
      }
      if let image = eachExercise["image"] as? String {
      exercise.imageName = image
      }
      if let explanation = eachExercise["explanation"] as? String {
      exercise.explanation = explanation
      }
      if let arrayWithAnswers = eachExercise["answers"] as? [[String : Any]] {
      for eachAnswer in arrayWithAnswers {
      if let answerText = eachAnswer["text"] as? String, let answerIsCorrect = eachAnswer["isCorrect"] as? Bool {
      let answer = Answer(context: backgroundContext)
      answer.text = answerText
      answer.isCorrect = answerIsCorrect
      exercise.addToAnswers(answer)
      }
      }
      }
      category.addToExercises(exercise)
      }
      }
      }
      try backgroundContext.save()
      userDefaults.set(true, forKey: preloadedDataKey)
      print("Sucess!")
      }
      catch {
      print("failed saving Context:(error.localizedDescription)")
      }

      }
      }
      }
      }
      }

      func applicationWillResignActive(_ application: UIApplication) {
      // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
      }

      func applicationDidEnterBackground(_ application: UIApplication) {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      }

      func applicationWillEnterForeground(_ application: UIApplication) {
      // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
      }

      func applicationDidBecomeActive(_ application: UIApplication) {
      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
      }

      func applicationWillTerminate(_ application: UIApplication) {
      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
      // Saves changes in the application's managed object context before the application terminates.
      self.saveContext()
      }

      // MARK: - Core Data stack

      lazy var persistentContainer: NSPersistentContainer = {
      /*
      The persistent container for the application. This implementation
      creates and returns a container, having loaded the store for the
      application to it. This property is optional since there are legitimate
      error conditions that could cause the creation of the store to fail.
      */
      let container = NSPersistentContainer(name: "StaplerCH")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
      if let error = error as NSError? {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

      /*
      Typical reasons for an error here include:
      * The parent directory does not exist, cannot be created, or disallows writing.
      * The persistent store is not accessible, due to permissions or data protection when the device is locked.
      * The device is out of space.
      * The store could not be migrated to the current model version.
      Check the error message to determine what the actual problem was.
      */
      fatalError("Unresolved error (error), (error.userInfo)")
      }
      })
      return container
      }()

      // MARK: - Core Data Saving support

      func saveContext () {
      let context = persistentContainer.viewContext
      if context.hasChanges {
      do {
      try context.save()
      } catch {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
      let nserror = error as NSError
      fatalError("Unresolved error (nserror), (nserror.userInfo)")
      }
      }
      }
      }


      Short workflow: After first installation, the app will load some demoQuestions from a plist into the persistent Store using the CoreData stack, so that the app is already preloaded with some Exercises, as said, in a later stage I'd like to be able to keep Exercises on a server from which the user can keep the Exercises updated.



      The App has 2 Modes, a Practice and an Exam Mode (depending of the mode the ExerciseVC behaves differently), I really tried hard to have one VC only for that.



      The other two Pages of the Start Page are quite irrelevant so far.



      I am very thankful for any input.










      share|improve this question









      New contributor




      paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am building a Quiz app in swift (my first app) to practice a few skills (using CoreData, working with plists, UIKit etc.) and later to work with a server which stores the Exercises.



      Here's my current point in development: https://bitbucket.org/paescebu/staplerch/src/master/



      My question is not precise, more general, I hope this is accepted here. I wanted to know if what I am practicing in my Code is good or bad practice so far.
      Any specifics like:
      - Design pattern
      - Conventions
      - bad practices
      are welcome



      example of my uncertainties (my AppDelegate):



      import UIKit
      import CoreData

      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {

      var window: UIWindow?

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Override point for customization after application launch.
      preloadData()
      return true
      }

      private func preloadData() {

      let preloadedDataKey = "didPreloadData"
      let userDefaults = UserDefaults.standard
      if userDefaults.bool(forKey: preloadedDataKey) == false
      {
      var categories: [Category] =
      if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist") {
      categories = readDemoDataPlist(withPath: path)
      }
      //preload into Core Data
      let backgroundContext = persistentContainer.newBackgroundContext()
      persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
      backgroundContext.perform
      {
      do {
      for item in categories {
      let category = CDCategory(context: backgroundContext)
      category.title = item.title
      category.imageName = item.imageName
      if let exerciseArray = item.exercises {
      for eachExercise in exerciseArray {
      let exercise = CDExercise(context: backgroundContext)
      exercise.question = eachExercise.question
      exercise.explanation = eachExercise.explanation
      exercise.imageName = eachExercise.imageName
      exercise.correctAnswer = eachExercise.correctAnswer
      if let answersArray = eachExercise.answers
      {
      for eachAnswer in answersArray {
      let answer = CDAnswers(context: backgroundContext)
      answer.answer = eachAnswer
      exercise.addToAnswers(answer)
      }
      }
      category.addToExercises(exercise)
      }
      }
      }
      try backgroundContext.save()
      userDefaults.set(true, forKey: preloadedDataKey)
      print("Sucess!")
      }
      catch {
      print("failed saving Context:(error.localizedDescription)")
      }

      }
      }
      }

      func readDemoDataPlist(withPath: String) -> [Category] {
      var categoriesArray : [Category] =
      if let arrayWithCategories = NSArray(contentsOfFile: withPath) as? [[String : Any]] {
      for eachCategory in arrayWithCategories {
      let category = Category()
      if let categoryTitle = eachCategory["title"] as? String {
      category.title = categoryTitle
      }
      if let categoryImage = eachCategory["imageName"] as? String {
      category.imageName = categoryImage
      }
      if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]] {
      var exerciseArray: [Exercise] =
      for eachExercise in arrayWithExercises {
      let exercise = Exercise()
      if let question = eachExercise["question"] as? String {
      exercise.question = question
      }
      if let correctAnswerIndex = eachExercise["correctAnswer"] as? String {
      exercise.correctAnswer = correctAnswerIndex
      }
      if let answers = eachExercise["answers"] as? [String] {
      exercise.answers = answers
      }
      if let image = eachExercise["image"] as? String {
      exercise.imageName = image
      }
      if let explanation = eachExercise["explanation"] as? String {
      exercise.explanation = explanation
      }
      exerciseArray.append(exercise)
      category.exercises = exerciseArray
      }
      }
      categoriesArray.append(category)
      }
      }
      return categoriesArray
      }

      func applicationWillResignActive(_ application: UIApplication) {
      // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
      }

      func applicationDidEnterBackground(_ application: UIApplication) {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      }

      func applicationWillEnterForeground(_ application: UIApplication) {
      // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
      }

      func applicationDidBecomeActive(_ application: UIApplication) {
      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
      }

      func applicationWillTerminate(_ application: UIApplication) {
      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
      // Saves changes in the application's managed object context before the application terminates.
      self.saveContext()
      }

      // MARK: - Core Data stack

      lazy var persistentContainer: NSPersistentContainer = {
      /*
      The persistent container for the application. This implementation
      creates and returns a container, having loaded the store for the
      application to it. This property is optional since there are legitimate
      error conditions that could cause the creation of the store to fail.
      */
      let container = NSPersistentContainer(name: "StaplerCH")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
      if let error = error as NSError? {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

      /*
      Typical reasons for an error here include:
      * The parent directory does not exist, cannot be created, or disallows writing.
      * The persistent store is not accessible, due to permissions or data protection when the device is locked.
      * The device is out of space.
      * The store could not be migrated to the current model version.
      Check the error message to determine what the actual problem was.
      */
      fatalError("Unresolved error (error), (error.userInfo)")
      }
      })
      return container
      }()

      // MARK: - Core Data Saving support

      func saveContext () {
      let context = persistentContainer.viewContext
      if context.hasChanges {
      do {
      try context.save()
      } catch {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
      let nserror = error as NSError
      fatalError("Unresolved error (nserror), (nserror.userInfo)")
      }
      }
      }
      }


      UPDATE



      I changed it quite a bit now, directly using the NSManagedObject model created from CoreData, instead of this step in between storing from plist, and the reading into persistent store.



      Looks now like this:



      import UIKit
      import CoreData

      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {
      var window: UIWindow?

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Override point for customization after application launch.
      preloadData()
      return true
      }

      private func preloadData() {

      let preloadedDataKey = "didPreloadData"
      let userDefaults = UserDefaults.standard
      if userDefaults.bool(forKey: preloadedDataKey) == false
      {
      if let path = Bundle.main.path(forResource: "demoExercises", ofType: "plist")
      {
      if let arrayWithCategories = NSArray(contentsOfFile: path) as? [[String : Any]]
      {
      //preload into Core Data
      let backgroundContext = persistentContainer.newBackgroundContext()
      persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
      backgroundContext.perform {
      do {
      for eachCategory in arrayWithCategories {
      let category = Category(context: backgroundContext)
      if let categoryTitle = eachCategory["title"] as? String {
      category.title = categoryTitle
      }
      if let categoryImage = eachCategory["imageName"] as? String {
      category.imageName = categoryImage
      }
      if let exerciseArray = eachCategory["exercises"] as? [[String: Any]] {
      for eachExercise in exerciseArray {
      let exercise = Exercise(context: backgroundContext)
      if let question = eachExercise["question"] as? String {
      exercise.question = question
      }
      if let image = eachExercise["image"] as? String {
      exercise.imageName = image
      }
      if let explanation = eachExercise["explanation"] as? String {
      exercise.explanation = explanation
      }
      if let arrayWithAnswers = eachExercise["answers"] as? [[String : Any]] {
      for eachAnswer in arrayWithAnswers {
      if let answerText = eachAnswer["text"] as? String, let answerIsCorrect = eachAnswer["isCorrect"] as? Bool {
      let answer = Answer(context: backgroundContext)
      answer.text = answerText
      answer.isCorrect = answerIsCorrect
      exercise.addToAnswers(answer)
      }
      }
      }
      category.addToExercises(exercise)
      }
      }
      }
      try backgroundContext.save()
      userDefaults.set(true, forKey: preloadedDataKey)
      print("Sucess!")
      }
      catch {
      print("failed saving Context:(error.localizedDescription)")
      }

      }
      }
      }
      }
      }

      func applicationWillResignActive(_ application: UIApplication) {
      // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
      // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
      }

      func applicationDidEnterBackground(_ application: UIApplication) {
      // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
      // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
      }

      func applicationWillEnterForeground(_ application: UIApplication) {
      // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
      }

      func applicationDidBecomeActive(_ application: UIApplication) {
      // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
      }

      func applicationWillTerminate(_ application: UIApplication) {
      // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
      // Saves changes in the application's managed object context before the application terminates.
      self.saveContext()
      }

      // MARK: - Core Data stack

      lazy var persistentContainer: NSPersistentContainer = {
      /*
      The persistent container for the application. This implementation
      creates and returns a container, having loaded the store for the
      application to it. This property is optional since there are legitimate
      error conditions that could cause the creation of the store to fail.
      */
      let container = NSPersistentContainer(name: "StaplerCH")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
      if let error = error as NSError? {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

      /*
      Typical reasons for an error here include:
      * The parent directory does not exist, cannot be created, or disallows writing.
      * The persistent store is not accessible, due to permissions or data protection when the device is locked.
      * The device is out of space.
      * The store could not be migrated to the current model version.
      Check the error message to determine what the actual problem was.
      */
      fatalError("Unresolved error (error), (error.userInfo)")
      }
      })
      return container
      }()

      // MARK: - Core Data Saving support

      func saveContext () {
      let context = persistentContainer.viewContext
      if context.hasChanges {
      do {
      try context.save()
      } catch {
      // Replace this implementation with code to handle the error appropriately.
      // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
      let nserror = error as NSError
      fatalError("Unresolved error (nserror), (nserror.userInfo)")
      }
      }
      }
      }


      Short workflow: After first installation, the app will load some demoQuestions from a plist into the persistent Store using the CoreData stack, so that the app is already preloaded with some Exercises, as said, in a later stage I'd like to be able to keep Exercises on a server from which the user can keep the Exercises updated.



      The App has 2 Modes, a Practice and an Exam Mode (depending of the mode the ExerciseVC behaves differently), I really tried hard to have one VC only for that.



      The other two Pages of the Start Page are quite irrelevant so far.



      I am very thankful for any input.







      beginner comparative-review swift ios core-data






      share|improve this question









      New contributor




      paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Dec 23 at 9:10









      Martin R

      15.5k12264




      15.5k12264






      New contributor




      paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Dec 21 at 10:45









      paescebu

      134




      134




      New contributor




      paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      paescebu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Coding style



          Generally your code is written clearly. Just two points that I noticed:





          • Two different styles of positioning the opening braces of a code block are used:



            if userDefaults.bool(forKey: preloadedDataKey) == false
            {


            vs



            if let exerciseArray = item.exercises {


            I prefer the second version, that is also what you'll find in the “Swift Programming Language” book and in the Swift source code. You can choose your favorite style, but it should be used consistently.




          • Testing boolean values: I prefer



            if !someBooleanExpression 


            over



            if someBooleanExpression == false



          Naming variables



          From the Swift API Design Guidelines,




          Name variables, parameters, and associated types according to their roles, rather than their type constraints.




          This applies to exerciseArray, answersArray etc in your code. Better choices would be exercises, answers. I would also omit the each prefix in eachExercise, eachAnswer, just



          for exercise in exercises { ... }


          In your first approach, where you have “plain” model classes in addition to the Core Data classes you could use the “cd” prefix to distinguish between the variables – as you already do to distinguish the classes:



          for exercise in exercises {
          let cdexercise = CDExercise(context: backgroundContext)
          ceexercise.question = exercise.question
          // ...
          }


          The Core Data models



          It seems that most (all?) attributes in the Core Data model classes are declared as optionals. But what happens if an attribute is not set? An exercise without question, an answer without text? It could lead to a runtime error later, or to unexpected results. You should check which attributes are required, and declare those as non-optionals.



          The isCorrect attribute of CDAnswer seems unnecessary since there is already a correctAnswer attribute in CDExercise. However, I would make that attribute a relationship to CDAnswer instead of a string.



          The property list



          From



          if let categoryImage = eachCategory["imageName"] as? String
          // ...
          if let image = eachExercise["image"] as? String {


          it seems that the keys in the default property list are not consistent. I'd suggest to use same names as the properties in the model classes, to avoid confusion.



          To force unwrap or not to force unwrap – loading resources



          You carefully use optional binding and conditional casts, which is generally a good practice to avoid runtime errors. However, when loading the property list, you know that it is present, and what it contains. Any error here would be a programming error and must be fixed before deploying the program.



          Therefore forced unwrapping/casting is actually better: It helps to detect the programming error early:



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          // ...


          The same applies to the optional casts: You know that category has a key "exercises" with a value which is an array of dictionaries, so instead of



          if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]]


          you can write



          let arrayWithExercises = eachCategory["exercises"] as! [[String : Any]]


          Approach #1: Loading the data into non-Core Data model objects first



          Your first approach is to load the default data into separate structures first, and then create the managed objects. That requires some extra space and time, but that matters only if the default data is big.



          It can be simplified considerably however, using a PropertyListDecoder. It suffices to declare conformance to the Decodable protocol in your plain model classes



          struct Category: Decodable {
          let title: String
          let imageName: String
          let exercises: [Exercise]
          }

          struct Exercise: Decodable { ... }

          struct Answer: Decodable { ... }


          and to make sure that the property names are identical to the keys in the property list file. Then reading the default data becomes as simple as



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListDecoder().decode([Category].self, from: data)


          The disadvantage of this approach is that you have to define extra types.



          Approach #2: Load the default data into Core Data directly



          I would use PropertyListSerialization to load the file (which works with both dictionaries and arrays):



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListSerialization.propertyList(from: data, format: nil)
          as! [[String: Any]]


          Instead of the addToXXX methods I would use the inverse relationships to connect the objects, e.g.



          category.addToExercises(exercise)


          becomes



          exercise.category = category


          Together with the previously mentioned points the loop to read the property list contents into Core Data would look like this:



          for category in categories {
          let cdcategory = Category(context: backgroundContext)
          cdcategory.title = category["title"] as! String
          cdcategory.imageName = category["imageName"] as! String

          for exercise in category["exercises"] as! [[String: Any]] {
          let cdexercise = Exercise(context: backgroundContext)
          cdexercise.category = cdcategory // Add exercise to the category
          cdexercise.question = exercise["question"] as! String
          // ...

          for answers in exercise["answers"] as! [[String: Any]] {
          let cdanswer = Answer(context: backgroundContext)
          cdanswer.exercise = cdexercise // Add answer to the exercise
          // ...
          }
          }
          }


          which is a bit shorter and easier to read than your original code.



          One could also try to load the managed objects directly from the property list as in approach #1. That requires to add Decodable conformance to the NSManagedObject subclasses. A possible solution is described in How to use swift 4 Codable in Core Data? on Stack Overflow. However, it requires to write all the



           convenience init(from decoder: Decoder)


          methods, so it may not be worth the effort.






          share|improve this answer





















          • Wow, this answer surpassed everything i expected from my question, thank you very much for going that deeply through my code, this helps me a lot! wish you merry christmas!
            – paescebu
            Dec 23 at 7:58










          • @paescebu: You are welcome – Merry Christmas!
            – Martin R
            Dec 23 at 9:10











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          paescebu is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210110%2fquiz-app-with-practice-and-exam-modes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Coding style



          Generally your code is written clearly. Just two points that I noticed:





          • Two different styles of positioning the opening braces of a code block are used:



            if userDefaults.bool(forKey: preloadedDataKey) == false
            {


            vs



            if let exerciseArray = item.exercises {


            I prefer the second version, that is also what you'll find in the “Swift Programming Language” book and in the Swift source code. You can choose your favorite style, but it should be used consistently.




          • Testing boolean values: I prefer



            if !someBooleanExpression 


            over



            if someBooleanExpression == false



          Naming variables



          From the Swift API Design Guidelines,




          Name variables, parameters, and associated types according to their roles, rather than their type constraints.




          This applies to exerciseArray, answersArray etc in your code. Better choices would be exercises, answers. I would also omit the each prefix in eachExercise, eachAnswer, just



          for exercise in exercises { ... }


          In your first approach, where you have “plain” model classes in addition to the Core Data classes you could use the “cd” prefix to distinguish between the variables – as you already do to distinguish the classes:



          for exercise in exercises {
          let cdexercise = CDExercise(context: backgroundContext)
          ceexercise.question = exercise.question
          // ...
          }


          The Core Data models



          It seems that most (all?) attributes in the Core Data model classes are declared as optionals. But what happens if an attribute is not set? An exercise without question, an answer without text? It could lead to a runtime error later, or to unexpected results. You should check which attributes are required, and declare those as non-optionals.



          The isCorrect attribute of CDAnswer seems unnecessary since there is already a correctAnswer attribute in CDExercise. However, I would make that attribute a relationship to CDAnswer instead of a string.



          The property list



          From



          if let categoryImage = eachCategory["imageName"] as? String
          // ...
          if let image = eachExercise["image"] as? String {


          it seems that the keys in the default property list are not consistent. I'd suggest to use same names as the properties in the model classes, to avoid confusion.



          To force unwrap or not to force unwrap – loading resources



          You carefully use optional binding and conditional casts, which is generally a good practice to avoid runtime errors. However, when loading the property list, you know that it is present, and what it contains. Any error here would be a programming error and must be fixed before deploying the program.



          Therefore forced unwrapping/casting is actually better: It helps to detect the programming error early:



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          // ...


          The same applies to the optional casts: You know that category has a key "exercises" with a value which is an array of dictionaries, so instead of



          if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]]


          you can write



          let arrayWithExercises = eachCategory["exercises"] as! [[String : Any]]


          Approach #1: Loading the data into non-Core Data model objects first



          Your first approach is to load the default data into separate structures first, and then create the managed objects. That requires some extra space and time, but that matters only if the default data is big.



          It can be simplified considerably however, using a PropertyListDecoder. It suffices to declare conformance to the Decodable protocol in your plain model classes



          struct Category: Decodable {
          let title: String
          let imageName: String
          let exercises: [Exercise]
          }

          struct Exercise: Decodable { ... }

          struct Answer: Decodable { ... }


          and to make sure that the property names are identical to the keys in the property list file. Then reading the default data becomes as simple as



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListDecoder().decode([Category].self, from: data)


          The disadvantage of this approach is that you have to define extra types.



          Approach #2: Load the default data into Core Data directly



          I would use PropertyListSerialization to load the file (which works with both dictionaries and arrays):



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListSerialization.propertyList(from: data, format: nil)
          as! [[String: Any]]


          Instead of the addToXXX methods I would use the inverse relationships to connect the objects, e.g.



          category.addToExercises(exercise)


          becomes



          exercise.category = category


          Together with the previously mentioned points the loop to read the property list contents into Core Data would look like this:



          for category in categories {
          let cdcategory = Category(context: backgroundContext)
          cdcategory.title = category["title"] as! String
          cdcategory.imageName = category["imageName"] as! String

          for exercise in category["exercises"] as! [[String: Any]] {
          let cdexercise = Exercise(context: backgroundContext)
          cdexercise.category = cdcategory // Add exercise to the category
          cdexercise.question = exercise["question"] as! String
          // ...

          for answers in exercise["answers"] as! [[String: Any]] {
          let cdanswer = Answer(context: backgroundContext)
          cdanswer.exercise = cdexercise // Add answer to the exercise
          // ...
          }
          }
          }


          which is a bit shorter and easier to read than your original code.



          One could also try to load the managed objects directly from the property list as in approach #1. That requires to add Decodable conformance to the NSManagedObject subclasses. A possible solution is described in How to use swift 4 Codable in Core Data? on Stack Overflow. However, it requires to write all the



           convenience init(from decoder: Decoder)


          methods, so it may not be worth the effort.






          share|improve this answer





















          • Wow, this answer surpassed everything i expected from my question, thank you very much for going that deeply through my code, this helps me a lot! wish you merry christmas!
            – paescebu
            Dec 23 at 7:58










          • @paescebu: You are welcome – Merry Christmas!
            – Martin R
            Dec 23 at 9:10
















          0














          Coding style



          Generally your code is written clearly. Just two points that I noticed:





          • Two different styles of positioning the opening braces of a code block are used:



            if userDefaults.bool(forKey: preloadedDataKey) == false
            {


            vs



            if let exerciseArray = item.exercises {


            I prefer the second version, that is also what you'll find in the “Swift Programming Language” book and in the Swift source code. You can choose your favorite style, but it should be used consistently.




          • Testing boolean values: I prefer



            if !someBooleanExpression 


            over



            if someBooleanExpression == false



          Naming variables



          From the Swift API Design Guidelines,




          Name variables, parameters, and associated types according to their roles, rather than their type constraints.




          This applies to exerciseArray, answersArray etc in your code. Better choices would be exercises, answers. I would also omit the each prefix in eachExercise, eachAnswer, just



          for exercise in exercises { ... }


          In your first approach, where you have “plain” model classes in addition to the Core Data classes you could use the “cd” prefix to distinguish between the variables – as you already do to distinguish the classes:



          for exercise in exercises {
          let cdexercise = CDExercise(context: backgroundContext)
          ceexercise.question = exercise.question
          // ...
          }


          The Core Data models



          It seems that most (all?) attributes in the Core Data model classes are declared as optionals. But what happens if an attribute is not set? An exercise without question, an answer without text? It could lead to a runtime error later, or to unexpected results. You should check which attributes are required, and declare those as non-optionals.



          The isCorrect attribute of CDAnswer seems unnecessary since there is already a correctAnswer attribute in CDExercise. However, I would make that attribute a relationship to CDAnswer instead of a string.



          The property list



          From



          if let categoryImage = eachCategory["imageName"] as? String
          // ...
          if let image = eachExercise["image"] as? String {


          it seems that the keys in the default property list are not consistent. I'd suggest to use same names as the properties in the model classes, to avoid confusion.



          To force unwrap or not to force unwrap – loading resources



          You carefully use optional binding and conditional casts, which is generally a good practice to avoid runtime errors. However, when loading the property list, you know that it is present, and what it contains. Any error here would be a programming error and must be fixed before deploying the program.



          Therefore forced unwrapping/casting is actually better: It helps to detect the programming error early:



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          // ...


          The same applies to the optional casts: You know that category has a key "exercises" with a value which is an array of dictionaries, so instead of



          if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]]


          you can write



          let arrayWithExercises = eachCategory["exercises"] as! [[String : Any]]


          Approach #1: Loading the data into non-Core Data model objects first



          Your first approach is to load the default data into separate structures first, and then create the managed objects. That requires some extra space and time, but that matters only if the default data is big.



          It can be simplified considerably however, using a PropertyListDecoder. It suffices to declare conformance to the Decodable protocol in your plain model classes



          struct Category: Decodable {
          let title: String
          let imageName: String
          let exercises: [Exercise]
          }

          struct Exercise: Decodable { ... }

          struct Answer: Decodable { ... }


          and to make sure that the property names are identical to the keys in the property list file. Then reading the default data becomes as simple as



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListDecoder().decode([Category].self, from: data)


          The disadvantage of this approach is that you have to define extra types.



          Approach #2: Load the default data into Core Data directly



          I would use PropertyListSerialization to load the file (which works with both dictionaries and arrays):



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListSerialization.propertyList(from: data, format: nil)
          as! [[String: Any]]


          Instead of the addToXXX methods I would use the inverse relationships to connect the objects, e.g.



          category.addToExercises(exercise)


          becomes



          exercise.category = category


          Together with the previously mentioned points the loop to read the property list contents into Core Data would look like this:



          for category in categories {
          let cdcategory = Category(context: backgroundContext)
          cdcategory.title = category["title"] as! String
          cdcategory.imageName = category["imageName"] as! String

          for exercise in category["exercises"] as! [[String: Any]] {
          let cdexercise = Exercise(context: backgroundContext)
          cdexercise.category = cdcategory // Add exercise to the category
          cdexercise.question = exercise["question"] as! String
          // ...

          for answers in exercise["answers"] as! [[String: Any]] {
          let cdanswer = Answer(context: backgroundContext)
          cdanswer.exercise = cdexercise // Add answer to the exercise
          // ...
          }
          }
          }


          which is a bit shorter and easier to read than your original code.



          One could also try to load the managed objects directly from the property list as in approach #1. That requires to add Decodable conformance to the NSManagedObject subclasses. A possible solution is described in How to use swift 4 Codable in Core Data? on Stack Overflow. However, it requires to write all the



           convenience init(from decoder: Decoder)


          methods, so it may not be worth the effort.






          share|improve this answer





















          • Wow, this answer surpassed everything i expected from my question, thank you very much for going that deeply through my code, this helps me a lot! wish you merry christmas!
            – paescebu
            Dec 23 at 7:58










          • @paescebu: You are welcome – Merry Christmas!
            – Martin R
            Dec 23 at 9:10














          0












          0








          0






          Coding style



          Generally your code is written clearly. Just two points that I noticed:





          • Two different styles of positioning the opening braces of a code block are used:



            if userDefaults.bool(forKey: preloadedDataKey) == false
            {


            vs



            if let exerciseArray = item.exercises {


            I prefer the second version, that is also what you'll find in the “Swift Programming Language” book and in the Swift source code. You can choose your favorite style, but it should be used consistently.




          • Testing boolean values: I prefer



            if !someBooleanExpression 


            over



            if someBooleanExpression == false



          Naming variables



          From the Swift API Design Guidelines,




          Name variables, parameters, and associated types according to their roles, rather than their type constraints.




          This applies to exerciseArray, answersArray etc in your code. Better choices would be exercises, answers. I would also omit the each prefix in eachExercise, eachAnswer, just



          for exercise in exercises { ... }


          In your first approach, where you have “plain” model classes in addition to the Core Data classes you could use the “cd” prefix to distinguish between the variables – as you already do to distinguish the classes:



          for exercise in exercises {
          let cdexercise = CDExercise(context: backgroundContext)
          ceexercise.question = exercise.question
          // ...
          }


          The Core Data models



          It seems that most (all?) attributes in the Core Data model classes are declared as optionals. But what happens if an attribute is not set? An exercise without question, an answer without text? It could lead to a runtime error later, or to unexpected results. You should check which attributes are required, and declare those as non-optionals.



          The isCorrect attribute of CDAnswer seems unnecessary since there is already a correctAnswer attribute in CDExercise. However, I would make that attribute a relationship to CDAnswer instead of a string.



          The property list



          From



          if let categoryImage = eachCategory["imageName"] as? String
          // ...
          if let image = eachExercise["image"] as? String {


          it seems that the keys in the default property list are not consistent. I'd suggest to use same names as the properties in the model classes, to avoid confusion.



          To force unwrap or not to force unwrap – loading resources



          You carefully use optional binding and conditional casts, which is generally a good practice to avoid runtime errors. However, when loading the property list, you know that it is present, and what it contains. Any error here would be a programming error and must be fixed before deploying the program.



          Therefore forced unwrapping/casting is actually better: It helps to detect the programming error early:



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          // ...


          The same applies to the optional casts: You know that category has a key "exercises" with a value which is an array of dictionaries, so instead of



          if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]]


          you can write



          let arrayWithExercises = eachCategory["exercises"] as! [[String : Any]]


          Approach #1: Loading the data into non-Core Data model objects first



          Your first approach is to load the default data into separate structures first, and then create the managed objects. That requires some extra space and time, but that matters only if the default data is big.



          It can be simplified considerably however, using a PropertyListDecoder. It suffices to declare conformance to the Decodable protocol in your plain model classes



          struct Category: Decodable {
          let title: String
          let imageName: String
          let exercises: [Exercise]
          }

          struct Exercise: Decodable { ... }

          struct Answer: Decodable { ... }


          and to make sure that the property names are identical to the keys in the property list file. Then reading the default data becomes as simple as



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListDecoder().decode([Category].self, from: data)


          The disadvantage of this approach is that you have to define extra types.



          Approach #2: Load the default data into Core Data directly



          I would use PropertyListSerialization to load the file (which works with both dictionaries and arrays):



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListSerialization.propertyList(from: data, format: nil)
          as! [[String: Any]]


          Instead of the addToXXX methods I would use the inverse relationships to connect the objects, e.g.



          category.addToExercises(exercise)


          becomes



          exercise.category = category


          Together with the previously mentioned points the loop to read the property list contents into Core Data would look like this:



          for category in categories {
          let cdcategory = Category(context: backgroundContext)
          cdcategory.title = category["title"] as! String
          cdcategory.imageName = category["imageName"] as! String

          for exercise in category["exercises"] as! [[String: Any]] {
          let cdexercise = Exercise(context: backgroundContext)
          cdexercise.category = cdcategory // Add exercise to the category
          cdexercise.question = exercise["question"] as! String
          // ...

          for answers in exercise["answers"] as! [[String: Any]] {
          let cdanswer = Answer(context: backgroundContext)
          cdanswer.exercise = cdexercise // Add answer to the exercise
          // ...
          }
          }
          }


          which is a bit shorter and easier to read than your original code.



          One could also try to load the managed objects directly from the property list as in approach #1. That requires to add Decodable conformance to the NSManagedObject subclasses. A possible solution is described in How to use swift 4 Codable in Core Data? on Stack Overflow. However, it requires to write all the



           convenience init(from decoder: Decoder)


          methods, so it may not be worth the effort.






          share|improve this answer












          Coding style



          Generally your code is written clearly. Just two points that I noticed:





          • Two different styles of positioning the opening braces of a code block are used:



            if userDefaults.bool(forKey: preloadedDataKey) == false
            {


            vs



            if let exerciseArray = item.exercises {


            I prefer the second version, that is also what you'll find in the “Swift Programming Language” book and in the Swift source code. You can choose your favorite style, but it should be used consistently.




          • Testing boolean values: I prefer



            if !someBooleanExpression 


            over



            if someBooleanExpression == false



          Naming variables



          From the Swift API Design Guidelines,




          Name variables, parameters, and associated types according to their roles, rather than their type constraints.




          This applies to exerciseArray, answersArray etc in your code. Better choices would be exercises, answers. I would also omit the each prefix in eachExercise, eachAnswer, just



          for exercise in exercises { ... }


          In your first approach, where you have “plain” model classes in addition to the Core Data classes you could use the “cd” prefix to distinguish between the variables – as you already do to distinguish the classes:



          for exercise in exercises {
          let cdexercise = CDExercise(context: backgroundContext)
          ceexercise.question = exercise.question
          // ...
          }


          The Core Data models



          It seems that most (all?) attributes in the Core Data model classes are declared as optionals. But what happens if an attribute is not set? An exercise without question, an answer without text? It could lead to a runtime error later, or to unexpected results. You should check which attributes are required, and declare those as non-optionals.



          The isCorrect attribute of CDAnswer seems unnecessary since there is already a correctAnswer attribute in CDExercise. However, I would make that attribute a relationship to CDAnswer instead of a string.



          The property list



          From



          if let categoryImage = eachCategory["imageName"] as? String
          // ...
          if let image = eachExercise["image"] as? String {


          it seems that the keys in the default property list are not consistent. I'd suggest to use same names as the properties in the model classes, to avoid confusion.



          To force unwrap or not to force unwrap – loading resources



          You carefully use optional binding and conditional casts, which is generally a good practice to avoid runtime errors. However, when loading the property list, you know that it is present, and what it contains. Any error here would be a programming error and must be fixed before deploying the program.



          Therefore forced unwrapping/casting is actually better: It helps to detect the programming error early:



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          // ...


          The same applies to the optional casts: You know that category has a key "exercises" with a value which is an array of dictionaries, so instead of



          if let arrayWithExercises = eachCategory["exercises"] as? [[String : Any]]


          you can write



          let arrayWithExercises = eachCategory["exercises"] as! [[String : Any]]


          Approach #1: Loading the data into non-Core Data model objects first



          Your first approach is to load the default data into separate structures first, and then create the managed objects. That requires some extra space and time, but that matters only if the default data is big.



          It can be simplified considerably however, using a PropertyListDecoder. It suffices to declare conformance to the Decodable protocol in your plain model classes



          struct Category: Decodable {
          let title: String
          let imageName: String
          let exercises: [Exercise]
          }

          struct Exercise: Decodable { ... }

          struct Answer: Decodable { ... }


          and to make sure that the property names are identical to the keys in the property list file. Then reading the default data becomes as simple as



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListDecoder().decode([Category].self, from: data)


          The disadvantage of this approach is that you have to define extra types.



          Approach #2: Load the default data into Core Data directly



          I would use PropertyListSerialization to load the file (which works with both dictionaries and arrays):



          let url = Bundle.main.url(forResource: "demoExercises", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let categories = try! PropertyListSerialization.propertyList(from: data, format: nil)
          as! [[String: Any]]


          Instead of the addToXXX methods I would use the inverse relationships to connect the objects, e.g.



          category.addToExercises(exercise)


          becomes



          exercise.category = category


          Together with the previously mentioned points the loop to read the property list contents into Core Data would look like this:



          for category in categories {
          let cdcategory = Category(context: backgroundContext)
          cdcategory.title = category["title"] as! String
          cdcategory.imageName = category["imageName"] as! String

          for exercise in category["exercises"] as! [[String: Any]] {
          let cdexercise = Exercise(context: backgroundContext)
          cdexercise.category = cdcategory // Add exercise to the category
          cdexercise.question = exercise["question"] as! String
          // ...

          for answers in exercise["answers"] as! [[String: Any]] {
          let cdanswer = Answer(context: backgroundContext)
          cdanswer.exercise = cdexercise // Add answer to the exercise
          // ...
          }
          }
          }


          which is a bit shorter and easier to read than your original code.



          One could also try to load the managed objects directly from the property list as in approach #1. That requires to add Decodable conformance to the NSManagedObject subclasses. A possible solution is described in How to use swift 4 Codable in Core Data? on Stack Overflow. However, it requires to write all the



           convenience init(from decoder: Decoder)


          methods, so it may not be worth the effort.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 22 at 23:09









          Martin R

          15.5k12264




          15.5k12264












          • Wow, this answer surpassed everything i expected from my question, thank you very much for going that deeply through my code, this helps me a lot! wish you merry christmas!
            – paescebu
            Dec 23 at 7:58










          • @paescebu: You are welcome – Merry Christmas!
            – Martin R
            Dec 23 at 9:10


















          • Wow, this answer surpassed everything i expected from my question, thank you very much for going that deeply through my code, this helps me a lot! wish you merry christmas!
            – paescebu
            Dec 23 at 7:58










          • @paescebu: You are welcome – Merry Christmas!
            – Martin R
            Dec 23 at 9:10
















          Wow, this answer surpassed everything i expected from my question, thank you very much for going that deeply through my code, this helps me a lot! wish you merry christmas!
          – paescebu
          Dec 23 at 7:58




          Wow, this answer surpassed everything i expected from my question, thank you very much for going that deeply through my code, this helps me a lot! wish you merry christmas!
          – paescebu
          Dec 23 at 7:58












          @paescebu: You are welcome – Merry Christmas!
          – Martin R
          Dec 23 at 9:10




          @paescebu: You are welcome – Merry Christmas!
          – Martin R
          Dec 23 at 9:10










          paescebu is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          paescebu is a new contributor. Be nice, and check out our Code of Conduct.













          paescebu is a new contributor. Be nice, and check out our Code of Conduct.












          paescebu is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210110%2fquiz-app-with-practice-and-exam-modes%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар