Lesson 4 changing variabels?

This lesson talks about Chris talks signing the model.get cards method into a new variabel, why?
from

let model = CardModel()

model.getCards

to

var cardsArray = Card

cardsArray = model.getCards

is it because the the function getCards returns the type of [Card]?

sorry for my bad English

class ViewController: UIViewController {

let model = CardModel()
var cardsArray = [Card]()


@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    collectionView.delegate = self
    collectionView.dataSource = self
  
    cardsArray = model.getCards()
    
}

Hi Nicholas,

As far as I understood, yes.

The “CardModel” is assigned to a constant “model” and the “.getCards()” method gets called to generate the cards in the empty array “cardsArray” using the class “Card”.

The class “Card” (simplified) has been defined as:

class Card {
    var imageName: String = ""
}

and the model “CardModel” (simplified) returning an array of cards containing “.imageName” Strings:

class CardModel {
    func getCards() -> [Card] {
            card1.imageName = "card\(random)"
            card2.imageName = "card\(random)"
            generatedCards += [card1, card2]
    }
    return generatedCards
}
1 Like