4 Replies
      Latest reply on May 19, 2020 2:33 PM by Claude31
      jrork Level 1 Level 1 (0 points)

        I'm working through the App Development with Swift, iOS 12 Edition and have run into a 'bug' in the Personality quiz at the end of Unit 3.  I've also downloaded the 'solution' from the teachers resources and I'm seeing the same experience.


        I believe the issue is in the lifecycle of a UIViewController and the variables that are initialized at creation.  The book suggests that an index is created in the "QuestionViewController" which is a UIViewController, and the index works fine on the first pass of the app.  The app consists of three view controllers, one 'intro', one 'question' and one 'results'.  The intro controller has a simple seque to the 'question' controller and doesn't pass in any information.  The 'question' controller presents the user with a list of questions that are then passed to the 'results' controller after all questions have been answered, a tally is caculated, and the results are displayed.  The 'results' controller also has a UIBarButtonItem as a 'Done' button that is control-dragged to the exit that goes to a function in the 'intro' controller called "unWindToQuizIntroduction" that has no logic built in.  I believe the expectation is that when the app is returned to the 'intro' controller, the 'question' controller has been eliminated and would be recreated on the segue to the (new) 'question' controler.


        However, it seems that the old 'question' controller is retained between segues as the index value that had been incremented is still the same value, which ends in an 'out of range' error when calling the question controller a second time.  As I said before, I compared 'my' code from the textbook against the project that was in the teachers manual, and I noticed two differences.  First, the index is created in a different swift file that also contains the question and answer arrays.  That shouldn't matter but I moved anyhow and results are the same.  Secondly, the 'intro' controller in the teachers manual has an overridden function 'didReceiveMemoryWarning' which also doesn't modify behavior.


        So the overall question is regarding the lifecycle of a viewController and the expectation of those values to be reinitialized.


        The project can be found in this zipfile under Unit 3->Personality Quiz