影片

課程範例

model,MemoryGame.swift

import Foundationstruct MemoryGame<CardContent> {
private(set) var cards: Array<Card>

func choose(_ card: Card) {

}

init(numberOfPairsOfCards: Int, createCardContent: (Int) -> CardContent) {
cards = Array<Card>()
for pairIndex in 0..<numberOfPairsOfCards {
let content = createCardContent(pairIndex)
cards.append(Card(content: content))
cards.append(Card(content: content))
}
}

struct Card {
var isFaceUp = true
var isMatched = false
var content: CardContent
}
}

view model,EmojiMemoryGame.swift

import Foundationclass EmojiMemoryGame {

static let emojis = ["🚲", "🚂", "🚁", "🚜", "🚕", "🏎", "🚑", "🚓", "🚒", "✈️", "🚀", "⛵️", "🛸", "🛶", "🚌", "🏍", "🛺", "🚠", "🛵", "🚗", "🚚", "🚇", "🛻", "🚝"]

static func createMemoryGame() -> MemoryGame<String> {
MemoryGame<String>(numberOfPairsOfCards: 4) { pairIndex in
emojis[pairIndex]
}
}

private var model = createMemoryGame()

var cards: Array<MemoryGame<String>.Card> {
return model.cards
}
}

view,ContentView.swift

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store