0

I am working on an educational flash card app which uses 4 cards. The 4 cards display images randomly pulled from separate arrays.

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:

//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)

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

Spits out something like this in the console area each time the deal button is pressed

c1 c3 c7 c18

c21 c12 c23 c2

c4 c6 c8 c11

etc etc etc etc

I think I need to figure out how to access that information to use in NSUserDefaults?

Here is what I've tried:

func savestate() {

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

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

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()

}

func loadstate() {


let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

Same thing here I know it's wrong was just trying stuff

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
        }

}

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.

My question is about what to save to save the picked cards using NSUserDefaults and not how to save images to NSUserDefaults

David Berry
  • 40,941
  • 12
  • 84
  • 95
Brad
  • 57
  • 1
  • 1
  • 7
  • I don't know as I'm not really trying to save the images themselves just the card strings? As in, save and load: card1String, card2String, card3String, card4String. – Brad Mar 15 '16 at 18:19
  • You do both in your example, trying to save images then strings. I assumed you needed the info in the other answer so I confirmed the suggested duplicate. If you need to save *strings*, then there's already many answers for this on SO. Tell us if you're still stuck after trying the solutions, we will reopen the question *if necessary*. – Eric Aya Mar 15 '16 at 18:24
  • Ok. Yeah that's what I tried because logically I thought I'm trying to save and load the current image but then realized that's not correct. I should have explained that in my question, I apologize. I'll search for saving strings, thank you! – Brad Mar 15 '16 at 18:27
  • No problem. You're welcome. – Eric Aya Mar 15 '16 at 18:30
  • My question should maybe be, "when deal button is pressed, random cards are dealt from array of image assets, how do I access which cards were dealt so I can use nsuserdefaults?" I've done tuts online, I get the concept of nsuserdefs and I've used it to save backgroundcolors , label/text in these tuts. I can't seem to figure out how to point it to what I need to save if that makes any sense? When I use print(cardString1, card2String etc) the info that prints out in the console area is actually what I need is it not? I have no idea how to get that info besides seeing it print in console. – Brad Mar 17 '16 at 18:33
  • Maybe just delete this post moderators, it's not doing anyone any good. Sorry. – Brad Mar 18 '16 at 04:13

0 Answers0