Posts

Showing posts from 2016

The Memento Pattern

Image
The memento pattern captures and externalizes an object’s internal state. In other words, it saves your stuff somewhere. Later on, this externalized state can be restored without violating encapsulation; that is, private data remains private. How to Use the Memento Pattern Add the following two methods to  ViewController.swift : //MARK: Memento Pattern func saveCurrentState ( ) { // When the user leaves the app and then comes back again, he wants //it to be in the exact same state // he left it. In order to do this we need to save the currently displayed album. // Since it's only one piece of information we can use NSUserDefaults. NSUserDefaults .standardUserDefaults ( ) .setInteger ( currentAlbumIndex, forKey : "currentAlbumIndex" ) }   func loadPreviousState ( ) { currentAlbumIndex = NSUserDefaults .standardUserDefaults ( ) .integerForKey ( "currentAlbumIndex" ) showDataForAlbum ( currentAlbumIndex ) } saveCurrentSt