MVC(Model - View Controller)
: ์ฝ๋๋ฅผ ์ฌ์ฌ์ฉํ๊ณ ์์๋ฅผ ๋ถ๋ฆฌํด์ ๋ชจ๋๋ก ๋ง๋๋๋ฐ ์ ์ฉํ ๋ฐฉ๋ฒ์ด๋ค.
Model: data & Logic
View: User Interface
Controller: Mediator
View ๋ Controller์๋ง ๋ํํ๋ค.
Controller๋ Model์์ ๊ฐ๋ง ๋ฐ์์์ ๋ณด์ฌ์ฃผ๊ธฐ๋ง ํ๋ค.
Model ์ด ๋ฐ์ดํฐ์ ๋ก์ง ๋ชจ๋๋ฅผ ๊ฐ๊ณ ์๋ค. ์ด๋ ค์ด๊ฑด ๋ค ๋ชจ๋ธ์ด ํ๋ค.
Quizzler ์ฑ ์ฝ๋๋ฅผ ์์๋ก ๋ค์ด๋ณด์.
์ผ์ชฝ์๋จ ์ ์, ๊ฐ์จ๋ฐ ํด์ฆ ๋ฌธ์ , ์๋ ๋ณด๊ธฐ 3๊ฐ, ๋งจ ์๋ ํ๋ก๊ทธ๋ ์ค๋ฐ๋ก ๊ตฌ์ฑ๋์ด ์๋ค.
๊ฐ์จ๋ฐ Text์์ ๋ฌธ์ ๊ฐ ๋์ค๊ณ ์๋์ ๋ณด๊ธฐ 3๊ฐ๊ฐ ๋์จ๋ค. ๋ณด๊ธฐ๋ ์ ๋ต์ ํด๋ฆญํ๋ฉด ์ด๋ก์, ์ค๋ต์ ํด๋ฆญํ๋ฉด ๋นจ๊ฐ์์ผ๋ก ๋ณํ๋ค.
๋ฌธ์ ๊ฐ ์งํ๋ ๋๋ง๋ค ํ๋ก๊ทธ๋ ์ค๋ฐ์ Score๊ฐ ๋ฐ๋๋ค.
ํ์ผ ๊ตฌ์กฐ๋ ์ด๋ ๊ฒ ์๊ฒผ๋ค.
Model(Question: Struct ๋ฌธ์ ๋ฐ์ดํฐ ๊ตฌ์กฐ, QuizBrain: ํด์ฆ ๋ฐ์ดํฐ, ํด์ฆ ์ ๋ต ์ ๋ฌด ํ๋จ, ํ์ฌ ๋ฒํธ ํ๋จ ๋ฑ์ ๋ก์ง),
View(ํ๋ฉด๋จ),
Controller(ViewController: QuizBrain ํด๋์ค์์ ๊ฐ์ ๋ฐ์์์ View์ ์ฐ๊ฒฐํด์ฃผ๋ ์ฝ๋๊ฐ ์์).
Question
import Foundation
struct Question{
let text: String
let answer: [String]
let correctAnswer: String
init(q: String, a: [String], correctAnswer: String){
text = q
answer = a
self.correctAnswer = correctAnswer
}
}
QuizBrain
import Foundation
struct QuizBrain{
let quiz = [
Question(q: "Which is the largest organ in the human body?", a: ["Heart", "Skin", "Large Intestine"], correctAnswer: "Skin"),
Question(q: "Five dollars is worth how many nickels?", a: ["25", "50", "100"], correctAnswer: "100"),
Question(q: "What do the letters in the GMT time zone stand for?", a: ["Global Meridian Time", "Greenwich Mean Time", "General Median Time"], correctAnswer: "Greenwich Mean Time"),
Question(q: "What is the French word for 'hat'?", a: ["Chapeau", "Écharpe", "Bonnet"], correctAnswer: "Chapeau"),
Question(q: "In past times, what would a gentleman keep in his fob pocket?", a: ["Notebook", "Handkerchief", "Watch"], correctAnswer: "Watch"),
Question(q: "How would one say goodbye in Spanish?", a: ["Au Revoir", "Adiós", "Salir"], correctAnswer: "Adiós"),
Question(q: "Which of these colours is NOT featured in the logo for Google?", a: ["Green", "Orange", "Blue"], correctAnswer: "Orange"),
Question(q: "What alcoholic drink is made from molasses?", a: ["Rum", "Whisky", "Gin"], correctAnswer: "Rum"),
Question(q: "What type of animal was Harambe?", a: ["Panda", "Gorilla", "Crocodile"], correctAnswer: "Gorilla"),
Question(q: "Where is Tasmania located?", a: ["Indonesia", "Australia", "Scotland"], correctAnswer: "Australia")
]
var questionNumber = 0
var score = 0
mutating func checkAnswer(_ userAnswer: String) -> Bool {
if userAnswer == quiz[questionNumber].correctAnswer{
score += 1
return true
}
else {
return false
}
}
func getAnswerText(index: Int) -> String{
return quiz[questionNumber].answer[index]
}
func getQuestionText() -> String {
return quiz[questionNumber].text
}
func getProgress() -> Float {
return Float(questionNumber + 1) / Float(quiz.count)
}
mutating func nextQuestion(){
if(questionNumber + 1 < quiz.count){
questionNumber += 1
}
else {
questionNumber = 0
score = 0
}
}
func getScore() -> Int{
return score
}
}
*mutating: ๊ฐ ํ์ ์ธ ๊ตฌ์กฐ์ฒด์์๋ ์ธ์คํด์ค ๋ฉ์๋ ๋ด์์ ํ๋กํผํฐ๋ค์ ์์ ํ ์ ์๊ฒ ๋์ด ์์ด์ ํ๋กํผํฐ๋ค์ ๊ตฌ์กฐ์ฒด ์์ ๋ฉ์๋์์ ์์ ํ๋ ค๋ฉด mutating์ด๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
Main(View)
ViewController
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var questionText: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var scoreText: UILabel!
@IBOutlet weak var selectionText1: UIButton!
@IBOutlet weak var selectionText2: UIButton!
@IBOutlet weak var selectionText3: UIButton!
var quizBrain = QuizBrain()
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
@IBAction func touchdownButton(_ sender: UIButton) {
let userAnswer = sender.currentTitle!
let userGotItRight = quizBrain.checkAnswer(userAnswer)
if userGotItRight {
sender.backgroundColor = UIColor.green
}
else{
sender.backgroundColor = UIColor.red
}
quizBrain.nextQuestion()
}
@IBAction func touchupButton(_ sender: UIButton) {
sender.backgroundColor = UIColor.clear
updateUI()
}
func updateUI(){
questionText.text = quizBrain.getQuestionText()
selectionText1.setTitle(quizBrain.getAnswerText(index: 0), for: .normal)
selectionText2.setTitle(quizBrain.getAnswerText(index: 1), for: .normal)
selectionText3.setTitle(quizBrain.getAnswerText(index: 2), for: .normal)
progressBar.progress = quizBrain.getProgress()
scoreText.text = "Score: \(quizBrain.getScore())"
}
}