Question: in swfit For this assignment, you will implement a model class to store this information. First, we will define a structure called Card to hold

in swfit For this assignment, you will implement a model class to store this information.

First, we will define a structure called Card to hold a question, corresponding answer and image names. A class or structure should reside in its own file.

You may define a custom Swift class or structure by following the steps below Choose: File -> New-> File choose iOS Source, Swift File

click Next

In the Save As field, type: Card (the default folder where the file is saved is the folder of your project)

click Create

We use the structure Card only to store a question, an answer and an image name. The content of the Card.swift file should be as follows.

import Foundation struct Card {

let image:String let question:String let answer:String

}

Next, we define a structure Deck to hold many cards. Made a file Deck.swift. Since we want the structure to support a type function getCard that returns the current card of the Deck, we will need a type variable named deck.

import Foundation

struct Deck { static var deck = [Card]() // a deck is an array of Card static var current:Int = 0 // index to the current card

init(){ // initialize your deck with Cards }// init

static func currentCard() -> Card { // return the current card in the deck return Deck.deck[current]

}

static func getCurrentIndex() -> Int {// return the index of the current card in the deck

return current }

// other helper functions

} // Deck

Using the singleton struct

Since we will refer to a unique Deck, we design the structure to be a singleton structure. This explains the code

static var deck = [Card]() You may initialize the structure with the code below (perhaps in the viewDidLoad

method)

_ = Deck()

We dont need to give the singleton a name because everything about it is a type var, or type function, so we will have to refer to the name of the structure anyway. For example, to get the current fruit, we would write

card = Deck.currentCard() 3. Handling persistent data

Use UserDefaults to relaunch the app with the current card. UserDefaults is a data area that stores persistent data that are not destroyed when an app terminates and can be retrieved when it relaunches. The following codes put the current card index into UserDefaults data area.

UserDefaults.standard.set(Deck.getCurrentIndex(), forKey: "currentIndex")

So, as you see, the unique keyword currentIndex will allow us to retrieve the value of the integer that is the index to the array of Cards.

To restore that Int from UserDefaults, we write: let i = UserDefaults.standard.integer(forKey: "currentIndex") as Int?

The objects (Int, or String, or arrays, etc. ) stored in UserDefaults must have distinct keys.

When to save data to UserDefaults

The scene delegate (probably called SceneDelegate.swift in your first assignment)

has a method called sceneDidDisconnect. This method is called by iOS after the app enters the background. You may save your current card index there.

The nice thing about singleton class/structure is the object/structure you define in the app delegate is the same object/structure that you manipulate in the view controller (nice, no?)

When to retrieve data from UserDefaults

In the method viewDidLoad of the view controller, you may restore the data from NSUserDefaults.

override func viewDidLoad() {

super.viewDidLoad()

// .....

if let i = UserDefaults.standard.integer(forKey: "currentIndex") as Int? { print("current index: \(i)") Deck.setCurrentIndex(to: i) //restore the current Card }

Deck.setCard(for: i) // set the current index for the deck // display the current card

} //viewDidLoad

Now, in the simulator, run the app, quit, and run it again. The current question and image should be shown when the app is relaunched. To quit an app in the simulator, press Shift-Command-H. You will have to press H twice to simulate the two presses on the home button. You will have to drag the app upward to quit it. Alternatively, you may press the Run button in Xcode to relaunch the app.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!