Shared Model In The AppDelegate L6.4.3

The AppDelegate, the delegate to the UIApplication object, is simplest and in most cases an adequate place to store shared model data because it can be accessed anywhere within the project (albeit controversial due to precedence of xcode's core data templates).

appdelegate

Memes on the AppDelegate

Pretty much the same as declaring a property

appdelegate property

Adding memes to the AppDelegate's memes array

Add this to the save function in the editor view controller

func save() {  
    // Create the meme
    let meme = Meme(top: topField.text!, bottom: bottomField.text!, image: imageView.image, memedImage: memedImage)

    // Add it to the memes array in the Application Delegate
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.memes.append(meme)
}

Accessing data from the collections or table view controller

class MemeCollectionViewController: UICollectionViewController {

    var memes: [Meme]!

    override func viewDidLoad() {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        memes = appDelegate.memes
    }
}