petervanhoef/Graphing-Calculator

Extra Credit 6: Show last graph when application first launches

petervanhoef opened this issue · 2 comments

When your application first launches, have it show the last graph it was showing (rather than coming up blank). You could reset the Calculator MVC upon relaunch to the last state it was in as well (which may or may not be the same thing as what the graph was showing). Be careful not to violate MVC in your solution, though (each MVC its its own independent world). The simplest persistence mechanism in iOS is UserDefaults, but only Property Lists can be put there, so you will have to figure out how to get the function you are graphing into a Property List-compatible form. Because of that, you are allowed use the Swift types Any or AnyObject to implement this Extra Credit item.

The UI tests are failing, as the app may start with the last graph. The current code is not able to find the button to go back to the calculator.

Possible solutions:

  1. Improve the test code to go back to the calculator
  2. Delete all UserDefaults settings. This has also the advantage that we always start a test in the same state.

Deleting all UserDefaults settings is not that difficult: see Deleting all NSUserDefaults with Swift. The code is very straightforward:

if let bundle = Bundle.main.bundleIdentifier { UserDefaults.standard.removePersistentDomain(forName: bundle) }

But calling this in the function override func setUp() doesn't seem to work. This is confirmed in the blogpost Xcode Automated UI Tests & NSUserDefaults. The post also suggests solutions, but they include modifying the app itself, which I hesitate to do.

There a better solutions that don't involve modifying the app:

The easiest solution for now is to use launch parameters. The solution to mock UserDefaults is from Test Driven Development more interesting as it enables us to extend the unit test to test if the settings are stored and retrieved.