๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] Design Patterns - MVC ํŒจํ„ด

ibelieveinme 2023. 4. 2. 01:21
728x90

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

 

 

728x90