1
\$\begingroup\$

I just want feedback about the code. Better implementation, more commentary etc.

The first storyboard

Second storyboard

  • app Delegate.swift

    import UIKit
    
    
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
            let savedResultVC = storyboard.instantiateViewController(withIdentifier: "SavedResultViewController") as? SavesResultsTableViewController
            let introductionVC = window?.rootViewController as? IntroductionViewController
            introductionVC?.delegate = savedResultVC
    
    
            return true
        }
    
  • Introduction view controller

    import UIKit
    
    class IntroductionViewController: UIViewController{
        var delegate: SavesResultsTableViewControllerDelegate?
    
        @IBOutlet weak var matematicQuizButton: UIButton!
        @IBOutlet weak var montryQuizButton: UIButton!
        override func viewDidLoad() {
            super.viewDidLoad()
            delegate?.setResult(result: nil)
            // Do any additional setup after loading the view.
        }
        override func prepare(for segue: UIStoryboardSegue, sender: Any?)
        {
    
            if segue.identifier == "ContryQuizSegue"
            {
                let navigationController = segue.destination as! UINavigationController
                let quizViewController = navigationController.topViewController as! QuizViewController
                quizViewController.matematicalQuizIsHidden = true
            }
    
    
        }
        @IBAction func unwindResultToMenue(unwindSegue: UIStoryboardSegue)
        {
        }
        @IBAction func unwindSavedResultToMenue(unwindSegue: UIStoryboardSegue)
        {
        }
    
  • QuizViewController

    import UIKit
    
    class QuizViewController: UIViewController {
    
        var matematicalExpressionAnswers = [MatematicalExpression]()
        var contryQuizAnswers = [Image_Contry]()
        var matematicalExpressionsNumber = 0
        var contrysNumber = 0
        var questionIndex = 1
        var timer = Timer()
        var time:Int = 0
        var matematicExpression:MatematicalExpression?
        var contry:Image_Contry?
    
    
        @IBOutlet weak var progressBar: UIProgressView!
    
        @IBOutlet weak var matematicalQuizStackview: UIStackView!
        @IBOutlet weak var contryQuizStackView: UIStackView!
    
        @IBOutlet weak var matematicalExpressionTimerLabel: UILabel!
        @IBOutlet weak var matematicalExpresionLabel: UILabel!
        @IBOutlet weak var matematicalExpressionTextField: UITextField!
    
        @IBOutlet weak var contryQuizTimerLabel: UILabel!
        @IBOutlet weak var contryQuizImageView: UIImageView!
        @IBOutlet var contryQuizAnswerChoicesButtons: [UIButton]!
    
        var matematicalQuizIsHidden:Bool?
        var matematicalExpressions:[MatematicalExpression] = [MatematicalExpression(expression: "2 × 2", answer: "4", playerCorectlyAnswered: false),
                                                              MatematicalExpression(expression: "5 + 2 × 6", answer: "17", playerCorectlyAnswered: false),
                                                              MatematicalExpression(expression: "6 - 3 × 8 ÷ 4", answer: "0", playerCorectlyAnswered: false),
                                                              MatematicalExpression(expression: "4 + 6 × 3 - 8", answer: "14", playerCorectlyAnswered: false),
                                                              MatematicalExpression(expression: "3 × 5 - 36 ÷ 4 + 1", answer: "7", playerCorectlyAnswered: false),
                                                              MatematicalExpression(expression: "5 + 3 - 9 × 2", answer: "-10", playerCorectlyAnswered: false)]
        // See pixeEb for image
        var contrys:[Image_Contry] = [Image_Contry(image: #imageLiteral(resourceName: "Pyramide"), answer: "Égypte", answerPossibility:["Cuba","Inde","Brésil","Égypte"] ,playerCorectlyAnswered: false),
                                      Image_Contry(image: #imageLiteral(resourceName: "Tour Eiffel"), answer: "France", answerPossibility: ["Angleterre","Russie","Allemagne","France"], playerCorectlyAnswered: false),
                                      Image_Contry(image: #imageLiteral(resourceName: "Machu Picchu"),answer:"Perou", answerPossibility: [ "Chili","Chine","Asie","Perou"], playerCorectlyAnswered: false),
                                      Image_Contry(image: #imageLiteral(resourceName: "Empire State Building"), answer: "Étas-Unis", answerPossibility: ["Mexique","Indonésie","Japon","Étas-Unis"], playerCorectlyAnswered: false),
                                      Image_Contry(image: #imageLiteral(resourceName: "gaspesie-1373018_1920"), answer: "Canada", answerPossibility: ["Grèce","Australie","Haïti","Canada"], playerCorectlyAnswered: false),
                                      Image_Contry(image: #imageLiteral(resourceName: "london-2393098_1920"), answer: "Angleterre", answerPossibility: ["Espagne","Italie","Corée du Sud","Angleterre"], playerCorectlyAnswered: false)]
        override func viewDidLoad() {
            super.viewDidLoad()
    
            if let hidden = matematicalQuizIsHidden
            {
                contrysNumber = contrys.count
                startContryTimer()
                matematicalQuizStackview.isHidden = hidden
                updateContryQuizUI()
    
            }
            else
            {
                matematicalExpressionsNumber = matematicalExpressions.count
                startMatematicalTimer()
                contryQuizStackView.isHidden = true
                updateMatematicalQuizUI()
    
            }
    
            // Do any additional setup after loading the view.
        }
    
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
        }
    
    
        @IBAction func playerGuessedAnswerMatematicalExpression(_ sender: UITextField) {
            guard let answer = sender.text else {return}
            guard !answer.isEmpty else {return}
    
            sender.isEnabled = false
    
            if sender.text == matematicExpression?.answer
            {
                sender.textColor = UIColor.green
                matematicExpression?.playerCorectlyAnswered = true
                matematicalExpressionAnswers.append(matematicExpression!)
    
    
            }
            else
            {
                sender.textColor = UIColor.red
                matematicExpression?.playerCorectlyAnswered = false
                matematicalExpressionAnswers.append(matematicExpression!)
            }
            stopTimer()
            nextMatematicalExpression()
        }
    
    
        //UI matematicalQuizModification
        func updateMatematicalQuizUI() -> Bool
        {
            guard matematicalExpressions.isEmpty == false else {return false}
    
            let number = Int.random(in: 0...matematicalExpressions.count - 1)
            matematicExpression = matematicalExpressions.remove(at: number)
            matematicalExpresionLabel.text = matematicExpression?.expression
            let totalProgress = Float(self.questionIndex)/Float(self.matematicalExpressionsNumber)
            self.progressBar.setProgress(totalProgress, animated: true)
            return true
        }
    
        func nextMatematicalExpression()
        {
    
            DispatchQueue.main.asyncAfter(deadline: .now() + 2)
            {
    
                self.questionIndex += 1
                self.matematicalExpressionTimerLabel.textColor = UIColor.black
                self.matematicalExpressionTextField.textColor = UIColor.black
                self.matematicalExpressionTextField.text = ""
    
    
                if self.updateMatematicalQuizUI()
                {
                    self.matematicalExpressionTimerLabel.text = "10"
                    self.startMatematicalTimer()
                    self.matematicalExpressionTextField.isEnabled = true
                }
                else
                {
                    self.matematicalExpressionTextField.isEnabled = false
                    self.performSegue(withIdentifier: "ResultSegue", sender: nil)
                }
            }
        }
        func startMatematicalTimer()
        {
            time = 10
            timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateMatematicalTimer), userInfo: nil, repeats: true)
        }
        func stopTimer()
        {
            timer.invalidate()
        }
    
        //text field func
    
    
        @objc func updateMatematicalTimer()
        {
            time -= 1
            matematicalExpressionTimerLabel.text = String(time)
            if time <= 0
            {                                                                                                                                                                                                                                                                                                                                                                                   stopTimer()
                self.matematicExpression?.playerCorectlyAnswered = false
                self.matematicalExpressionTextField.isEnabled = false
                matematicalExpressionTimerLabel.textColor = UIColor.red
                matematicalExpressionAnswers.append(matematicExpression!)
                nextMatematicalExpression()
            }
        }
        //ContryUI function
    
        func startContryTimer()
        {
            time = 10
            timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateContryTimer) , userInfo: nil, repeats: true)
        }
        @objc func updateContryTimer()
        {
            time -= 1
            contryQuizTimerLabel.text = String(time)
            if time == 0
            {                                                                                                                                                                                                                                                                                                                                                                                   stopTimer()
                contry?.playerCorectlyAnswered = false
                for button in self.contryQuizAnswerChoicesButtons
                {
                    button.isEnabled = false
                }
                contryQuizTimerLabel.textColor = UIColor.red
                contryQuizAnswers.append(contry!)
                nextContry()
            }
        }
        func updateContryQuizUI() -> Bool
        {
            guard !contrys.isEmpty else {return false}
    
            let number = Int.random(in: 0...contrys.count - 1)
            contry = contrys.remove(at: number)
            contryQuizImageView.image = contry?.image
            updateContryQuizButton(contry: contry!, buttons: contryQuizAnswerChoicesButtons)
            let totalProgress = Float(self.questionIndex)/Float(self.contrysNumber)
            self.progressBar.setProgress(totalProgress, animated: true)
    
            return true
        }
    
        func nextContry()
        {
            DispatchQueue.main.asyncAfter(deadline: .now() + 2)
            {
                self.questionIndex += 1
                self.contryQuizTimerLabel.textColor = UIColor.black
                for button in self.contryQuizAnswerChoicesButtons
                {
                    button.titleLabel?.textColor = UIColor.black
                }
    
                for button in self.contryQuizAnswerChoicesButtons
                {
                    button.isEnabled = true
                    button.setTitleColor(.black, for: .normal)
                }
    
                if self.updateContryQuizUI()
                {
                    self.contryQuizTimerLabel.text = "10"
                    self.startContryTimer()
                }
                else
                {
                    for button in self.contryQuizAnswerChoicesButtons
                    {
                        button.isEnabled = false
                    }
                    self.questionIndex = 0
                    self.performSegue(withIdentifier: "ResultSegue", sender:nil)
                }
            }
        }
        @IBAction func playerAnswerContryQuiz(_ sender: UIButton) {
            guard let answer = sender.titleLabel?.text else {return}
    
            for button in self.contryQuizAnswerChoicesButtons
            {
                button.isEnabled = false
    
            }
            if answer == contry?.answer
            {
                sender.setTitleColor(.green, for: .normal)
                contry?.playerCorectlyAnswered = true
                contryQuizAnswers.append(contry!)
            }
            else
            {
                sender.setTitleColor(.red, for: .normal)
                contry?.playerCorectlyAnswered = false
                contryQuizAnswers.append(contry!)
            }
            stopTimer()
            nextContry()
        }
    
        //prepare for segue func
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
            let resultViewController = segue.destination as? ResultViewController
            if matematicalQuizStackview.isHidden == true
            {
                resultViewController?.contryAnswers = contryQuizAnswers
                resultViewController?.matematicalAnswers = nil
    
            }
            else
            {
                resultViewController?.matematicalAnswers = matematicalExpressionAnswers
                resultViewController?.contryAnswers = nil
            }
    
            let resultVC = segue.destination as? ResultViewController
            let savedResultVC = storyboard?.instantiateViewController(withIdentifier: "SavedResultViewController") as? SavesResultsTableViewController
    
            resultVC?.delegate = savedResultVC
        }
    
  • Result ViewController

    import UIKit
    
    protocol SavesResultsTableViewControllerDelegate
    {
        func setResult(result: QuizResult?)
    }
    
    class ResultViewController: UIViewController {
    
        var delegate: SavesResultsTableViewControllerDelegate?
        var matematicalAnswers:[MatematicalExpression]?
        var contryAnswers:[Image_Contry]?
        var goodAnswersNumber:Double
        {
            //Calcaulating the number of good answer
            var goodAnswers = 0
            if let answers = matematicalAnswers
            {
                for answer in answers
                {
                    if answer.playerCorectlyAnswered == true
                    {
                        goodAnswers += 1
                    }
                }
            }
            if let answers = contryAnswers
            {
                for answer in answers
                {
                    if answer.playerCorectlyAnswered == true
                    {
                        goodAnswers += 1
                    }
                }
            }
            return Double(goodAnswers)
        }
        var result:String
        {
            var results = ""
            if let answers = matematicalAnswers
            {
                //Calculating the answer pourcentage
                let goodAnswerPourcentage = goodAnswersNumber/Double(answers.count)
                if goodAnswerPourcentage < 0.25
                {
                    results = "Stupide"
                }
                else if goodAnswerPourcentage >= 0.25 && goodAnswerPourcentage < 0.5
                {
                    results = "Dans la moyenne"
                }
                else if goodAnswerPourcentage >= 0.50 && goodAnswerPourcentage < 0.75
                {
                    results = "In the averge"
                }
                else if goodAnswerPourcentage >= 0.75 && goodAnswerPourcentage < 1
                {
                    results = "Au dessu de la moyenne"
                }
                else if goodAnswerPourcentage == 1
                {
                    results = "Génie"
                }
    
            }
            else if let answer = contryAnswers
            {
                let goodAnswerPourcentage = goodAnswersNumber/Double(answer.count)
                if goodAnswerPourcentage < 0.25
                {
                    results = "Stupide"
                }
                else if goodAnswerPourcentage >= 0.25 && goodAnswerPourcentage < 0.5
                {
                    results = "Dans la moyenne"
                }
                else if goodAnswerPourcentage >= 0.50 && goodAnswerPourcentage < 1
                {
                    results = "Au dessu de la moyenne"
                }
                else if goodAnswerPourcentage == 1
                {
                    results = "Génie"
                }
    
            }
    
            return results
        }
        var resultText:String
        {
            var resultText = ""
            // define the result
            switch result
            {
            case "Stupide":
                resultText = "Vous avez peut être besoin de reviser vos mathématique ou d'appronfondir vos connaissance générale"
            case "Dans la moyenne":
                resultText = "Vous n'ête pas spécialement intelligent mais pas si idiot."
            case "Au dessu de la moyenne":
                resultText = "Vous êtes supérieur à la moyenne"
            case "Génie":
                resultText = "Vous mériter de controller le monde tellement vos faculté intelectuelle sont élevé"
            default:
                resultText = "Il y a eu une erreur"
            }
            return resultText
        }
        var quizResult:QuizResult
        {
            var type = ""
            if matematicalAnswers != nil
            {
                type = "Matematical Quiz"
            }
            if contryAnswers != nil
            {
                type = "Contry Quiz"
            }
            return QuizResult(quizType:type, result: result)
        }
        @IBOutlet weak var resultLabel: UILabel!
        @IBOutlet weak var resultDescriptionTextView: UITextView!
        @IBOutlet weak var resultTableView: UITableView!
    
        var tableViewDataSource = ResultTableViewDataSource()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            tableViewDataSource.matematicalResults = matematicalAnswers
            tableViewDataSource.contryResults = contryAnswers
            self.resultLabel.text = result
            self.resultDescriptionTextView.text = resultText
            self.resultTableView.dataSource = tableViewDataSource
            delegate?.setResult(result: quizResult)
    
            // Do any additional setup after loading the view.
        }
    
  • SavesResultTableViewController

    import UIKit
    
    class SavesResultsTableViewController: UITableViewController, SavesResultsTableViewControllerDelegate {
    
        func setResult(result: QuizResult?)
        {
            if let savedResult = QuizResult.loadFromFile()
            {
                self.results = savedResult
            }
            if let result = result
            {
                self.results.append(result)
            }
        }
        var results = [QuizResult]()
        {
    
            didSet
            {
                QuizResult.saveToFile(quizResults: results)
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            if let savedResult = QuizResult.loadFromFile()
            {
                self.results = savedResult
            }
            // Uncomment the following line to preserve selection between presentations
            // self.clearsSelectionOnViewWillAppear = false
    
            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem
    
        }
    
        // MARK: - Table view data source
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of rows
    
            return results.count
    
        }
    
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "savedResult", for: indexPath)
    
            cell.textLabel?.text = "\(indexPath.row + 1). \(results[indexPath.row].quizType)"
            cell.detailTextLabel?.text = "\(results[indexPath.row].result)"
            return cell
        }
    
  • ResultTableViewDataSource

    import UIKit
    
    class ResultTableViewDataSource: NSObject, UITableViewDataSource {
    
        var matematicalResults:[MatematicalExpression]?
        var contryResults:[Image_Contry]?
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            var resultsCount = 0
            if let results = matematicalResults
            {
                resultsCount = results.count
            }
            else if let results = contryResults
            {
                resultsCount = results.count
            }
            return resultsCount
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "resultCell") as! ResultTableViewCell
    
            if let results = matematicalResults
            {
                cell.contryQuizStackView.isHidden = true
                let matematicalExpression = results[indexPath.row]
    
                cell.matematicalExpressionTrueOrFalseAnswer.textColor = matematicalExpression.playerCorectlyAnswered == true ? UIColor.green:UIColor.red
    
                cell.updateMatematicalExpressionStackViewContent(matematicalExpression: matematicalExpression.expression , trueOrFalse: matematicalExpression.playerCorectlyAnswered == true ? "✔︎":"✖︎")
    
            }
            else if let results = contryResults
            {
                cell.matematicalStackView.isHidden = true
                let contry = results[indexPath.row]
    
                cell.contryTrueOrFalseAnswer.textColor = contry.playerCorectlyAnswered == true ? UIColor.green:UIColor.red
    
                cell.updateContryStackViewContent(image: contry.image, trueOrFalse: contry.playerCorectlyAnswered == true ? "✔︎":"✖︎")
    
            }
            return cell
        }
    
  • ResultTableViewCell

    {
        @IBOutlet weak var contryQuizStackView: UIStackView!
        @IBOutlet weak var contryImage: UIImageView!
        @IBOutlet weak var contryTrueOrFalseAnswer: UILabel!
    
    
        @IBOutlet weak var matematicalStackView: UIStackView!
        @IBOutlet weak var matematicalExpressionLabel: UILabel!
        @IBOutlet weak var matematicalExpressionTrueOrFalseAnswer: UILabel!
    
    
        func updateContryStackViewContent(image:UIImage, trueOrFalse:String)
        {
            contryImage.image = image
            contryTrueOrFalseAnswer.text = trueOrFalse
        }
        func updateMatematicalExpressionStackViewContent(matematicalExpression: String, trueOrFalse: String)
        {
            matematicalExpressionLabel.text = matematicalExpression
            matematicalExpressionTrueOrFalseAnswer.text = trueOrFalse
    
        }
    }
    
  • MatematicalExpression

    import Foundation
    struct MatematicalExpression {
        var expression:String
        var answer:String
        var playerCorectlyAnswered = false
    }
    
  • Image_Contry

    import UIKit
    import Foundation
    struct Image_Contry
    {
        var image:UIImage
        var answer:String
        var answerPossibility:[String]
        var playerCorectlyAnswered = false
    
    
    }
    
  • QuizResult

    import Foundation
    
    class QuizResult: Codable
    {
        var quizType:String
        var result:String
        static var documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        static var archiveURL = QuizResult.documentDirectory.appendingPathComponent("QuizResult").appendingPathExtension("plist")
        init(quizType:String,result:String)
        {
            self.quizType = quizType
            self.result = result
        }
    
        static func saveToFile(quizResults:[QuizResult])
        {
            let quizResultEncoder = PropertyListEncoder()
            let encodedQuizResult = try? quizResultEncoder.encode(quizResults)
            try? encodedQuizResult?.write(to: archiveURL)
        }
        static func loadFromFile() -> [QuizResult]?
        {
            let quizResultDecoder = PropertyListDecoder()
    
            if let retrivedQuizResults = try? Data(contentsOf: archiveURL)
            {
                if let decodedQuizResults = try? quizResultDecoder.decode(Array<QuizResult>.self, from: retrivedQuizResults)
                {
                    return decodedQuizResults
                }
            }
            return nil
        }
    }
    
  • UpdateContryQuizButton

    import UIKit
    
    // It was a test to see if I can put function in other file
    func updateContryQuizButton(contry:Image_Contry,buttons:[UIButton])
    {
        var answerPossibilitys = contry.answerPossibility
    
        for button in buttons
        {
            let number = Int.random(in: 0...answerPossibilitys.count - 1)
            button.setTitle(answerPossibilitys.remove(at: number), for: .normal)
        }
    
    }
    
\$\endgroup\$
2
\$\begingroup\$

First of all I want to mention that I used Swift 4.2/Xcode 10.1 to write this answer.

You should have this extension in your project, count(of:) returns count of elements in a sequence that fit to your statement:

extension Sequence {
    func count(where predicate: (Element) -> Bool) -> Int {
        return reduce(0) { currentResult, currentItem in
            return currentResult + (predicate(currentItem) ? 1 : 0)
        }
    }
}

As far as I can see in your code, MatematicalExpression and Image_Contry are very similar. You can declare a protocol that demonstrates this feature:

protocol AnswerDataProvider {
    var answer: String { get }
    var playerCorectlyAnswered: Bool { get }
}

MatematicalExpression and Image_Contry should implement this protocol:

extension MatematicalExpression: AnswerDataProvider { }

extension Image_Contry: AnswerDataProvider { }

With this extension you can easily get count of correct answers:

extension Sequence where Element: AnswerDataProvider {
    var countOfCorrectAnswers: Int {
        return count { $0.playerCorectlyAnswered }
    }
}

You have a lot of similar strings, use this enum to solve the problem:

enum Comment {
    case stupid
    case belowAverage
    case average
    case aboveAverage
    case genius

    var translation: String {
        switch self {
        case .stupid:
            return "Stupide"
        case .belowAverage:
            return "Dans la moyenne"
        case .average:
            return "In the averge"
        case .aboveAverage:
            return "Au dessu de la moyenne"
        case .genius:
            return "Génie"
        }
    }
}

With this enum you will have only 1 property for matematicalAnswers and contryAnswers in ResultViewController:

enum Answers {
    case matematical([MatematicalExpression])
    case country([Image_Contry])
}

I guess these functions can help you to solve different problems:

extension Answers {
    var countOfAllAnswers: Int {
        switch self {
        case .matematical(let matematicalExpressions):
            return matematicalExpressions.count
        case .country(let contryAnswers):
            return contryAnswers.count
        }
    }

    var countOfCorrectAnswers: Int {
        switch self {
        case .matematical(let matematicalExpressions):
            return matematicalExpressions.countOfCorrectAnswers
        case .country(let contryAnswers):
            return contryAnswers.countOfCorrectAnswers
        }
    }

    var goodAnswerPercentage: Double {
        return Double(countOfCorrectAnswers)/Double(countOfAllAnswers)
    }

    var percentageWithComments: [(Double, Comment)] {
        switch self {
        case .matematical:
            return [
                (0.0, .stupid),
                (0.25, .belowAverage),
                (0.5, .average),
                (0.75, .aboveAverage),
                (1.0, .genius)
            ]
        case .country:
            return [
                (0.0, .stupid),
                (0.25, .belowAverage),
                (0.5, .aboveAverage),
                (1.0, .genius)
            ]
        }
    }

    var result: String {
        let goodAnswerPercentageValue = goodAnswerPercentage
        return percentageWithComments.reversed().first {
            return goodAnswerPercentageValue >= $0.0
        }?.1.translation ?? ""
    }

    var quizResult: QuizResult {
        let type: String
        switch self {
        case .matematical:
            type = "Matematical Quiz"
        case .country:
            type = "Contry Quiz"
        }
        return QuizResult(quizType: type, result: result)
    }
}

In such case ResultViewController will be shorter:

class ResultViewController: UIViewController {

    var answers: Answers?
    var goodAnswersNumber: Double {
        return Double(answers?.countOfCorrectAnswers ?? 0)
    }

    var result: String {
        return answers?.result ?? ""
    }

    var quizResult: QuizResult {
        return answers?.quizResult ?? QuizResult(quizType: "", result: result)
    }

    //Other functions and properties
}

You can try to refactor other controllers with the similar protocols and enums.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer but I dont understand the "where predicate: (Element)" \$\endgroup\$ – C.Calvinno Jan 21 at 16:47
  • \$\begingroup\$ @C.Calvinno this function returns count of items that fit some statement. In your case you can use it for the count of correct answers. For more information check these links: forums.swift.org/t/count-where-on-sequence/11186 and github.com/apple/swift-evolution/blob/master/proposals/… . \$\endgroup\$ – Roman Podymov Jan 21 at 17:32
  • 1
    \$\begingroup\$ Hello @C.Calvinno. Do you still have questions regarding the func count(where:)? \$\endgroup\$ – Roman Podymov Jan 23 at 19:22
  • \$\begingroup\$ No ,thanks, I had some question about the where keyword but I find the information on the internet. Now I gradually understand your way of coding and change mine \$\endgroup\$ – C.Calvinno Jan 23 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.