NSUser Defaults Swift (random flash cards)
<p>I am working on an educational flash card app which uses 4 cards. The 4 cards display images randomly pulled from separate arrays. </p>

<p>I'm looking for a way to have it save the current cards that get dealt if the user leaves/closes the app, comes back the next day and is able to continue where they left off.  Here is the code for dealing cards:</p>

<pre><code>//Randomize card 1
        let card1Random:Int = Int(arc4random_uniform(3))
        let card1String:String = self.array1[card1Random]
    //Randomize card 2
        let card2Random:Int = Int(arc4random_uniform(1))
        let card2String:String = self.array2[card2Random]
    //Randomize card 3
        let card3Random:Int = Int(arc4random_uniform(4))
        let card3String:String = self.array3[card3Random]
    //Randomize card 4
        let card4Random:Int = Int(arc4random_uniform(1))
        let card4String:String = self.array4[card4Random]
    //Setting each beat view to correspond to the randomize patterns
        self.card1.image = UIImage(named: card1String)
        self.card2.image = UIImage(named: card2String)
        self.card3.image = UIImage(named: card3String)
        self.card4.image = UIImage(named: card4String)
print(card1String, card2String, card3String, card4String)
</code></pre>

<p>So this here—>  print(card1String, card2String, card3String, card4String) </p>

<p>Spits out something like this in the console area each time the deal button is pressed</p>

<p>c1 c3 c7 c18</p>

<p>c21 c12 c23 c2</p>

<p>c4 c6 c8 c11</p>

<p>etc etc etc etc</p>

<p>I think I need to figure out how to access that information to use in NSUserDefaults?</p>

<p>Here is what I've tried:</p>

<pre><code>func savestate() {

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
</code></pre>

<p>It's this next part I don't know how to call on the info needed, I know is is wrong I was just trying stuff</p>

<pre><code>defaults.setObject(self.crd1.image, forKey: "card1")
defaults.setObject(self.crd2.image, forKey: "card2")
defaults.setObject(self.crd3.image, forKey: "card3")
defaults.setObject(self.crd4.image, forKey: "card4")
defaults.synchronize()
</code></pre>

<p>}</p>

<pre><code>func loadstate() {


let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
</code></pre>

<p>Same thing here I know it's wrong was just trying stuff</p>

<pre><code>if let _ = defaults.objectForKey("card1") as? String {
            self.card1.image = defaults.objectForKey("card1") as! UIImage
        }
        if let _ = defaults.objectForKey("card2") as? String {
            self.card2.image = defaults.objectForKey("card2") as! UIImage
        }
        if let _ = defaults.objectForKey("card3") as? String {
            self.card3.image = defaults.objectForKey("card3") as! UIImage
        }
        if let _ = defaults.objectForKey("card3") as? String {
            self.card4.image = defaults.objectForKey("card3") as! UIImage
        }
</code></pre>

<p>}         </p>

<p>Instead of self.card1.image = etc I tried card1String.  I really am having trouble understanding how to use nsuser defaults in this compared to just having it save a background color chosen or high score like the tutorials I've done online.  I don't know how to have it store the cards currently displayed on screen. </p>

<p>My question is about what to save to save the picked cards using <code>NSUserDefaults</code> and not how to save images to <code>NSUserDefaults</code></p>