UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Confusion about refactoring, project 5, challenge 2

Forums > 100 Days of Swift

@onqun  

Hello everyone, I feel like i did not understand refactoring. This is what I did for error messages. Is it ok?

import UIKit

class ViewController: UITableViewController {
    var allWords = [String]()
    var usedWords = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(promptForAnswer))

        if let startWordsURL = Bundle.main.url(forResource: "start", withExtension: "txt") {
            if let startWords = try? String(contentsOf: startWordsURL){
                allWords = startWords.components(separatedBy: "\n")
            }
        }

        if allWords.isEmpty {
            allWords = ["Silkworm"]
        }

        startGame()
    }

    func startGame() {
        title = allWords.randomElement()
        usedWords.removeAll(keepingCapacity: true)
        tableView.reloadData()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return usedWords.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Word", for: indexPath)
        cell.textLabel?.text = usedWords[indexPath.row]
        return cell
    }

    @objc func promptForAnswer() {
        let ac = UIAlertController(title: "Enter Answer", message: nil, preferredStyle: .alert)
        ac.addTextField()

        let submitAction = UIAlertAction(title: "Submit", style: .default) {
            [weak self, weak ac] _ in
            guard let answer = ac?.textFields?[0].text else { return}
            self?.submit(answer)
        }
        ac.addAction(submitAction)
        present(ac, animated: true)
    }

    func submit(_ answer: String) {
        let lowerAnswer = answer.lowercased()

       // let errorTitle: String
      //  let errorMessage: String

        if isPossible(word: lowerAnswer){
            if isOriginal(word: lowerAnswer){
                if isReal(word: lowerAnswer){
                    if isShort(word: lowerAnswer){

                        usedWords.insert(answer, at:0)

                        let indexPath = IndexPath(row: 0 , section: 0)
                        tableView.insertRows(at: [indexPath], with: .automatic)
                        return
                    } else {
                        showErrorMessage(title:  "Word whould be more than 3 letters", message: " too Short Bitch")
                        //errorTitle = "Word whould be more than 3 letters"
                        //errorMessage = " too Short Bitch"
                    }
                }else {
                    showErrorMessage(title:  "word not recognized", message: "you cant just make them up")

                    //errorTitle = "word not recognized"
                    //errorMessage = "you cant just make them up"
                }
            }else{

                showErrorMessage(title:  "Word already used", message: "Be more Original")
               // errorTitle = "Word already used"
               // errorMessage = "Be more Original"
            }
        } else {
            showErrorMessage(title: "Word not possible", message: "You cant spell that word from \(title!.lowercased())")
           // errorTitle = "word not possible"
           // errorMessage = "You cant spell that word from \(title!.lowercased())"
        }
        //let ac = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
        //ac.addAction(UIAlertAction(title: "OK", style: .default))
       // present(ac, animated: true)
    }

    func isPossible(word:String) -> Bool {
        guard var tempWord = title?.lowercased() else { return false }
        for letter in word {
            if let position = tempWord.firstIndex(of: letter) {
                tempWord.remove(at: position)
            }else {
                return false
            }
        }
        return true
    }

    func isOriginal(word: String) -> Bool {
        return !usedWords.contains(word)
    }

    func isReal(word: String) -> Bool {
        let checker = UITextChecker()
        let range = NSRange( location: 0, length: word.utf16.count)
        let misspelledRange = checker.rangeOfMisspelledWord( in: word, range: range, startingAt: 0 , wrap: false, language: "en")
        return misspelledRange.location == NSNotFound
    }

    func isShort(word: String) -> Bool {
        let shortWord = word.count
        if shortWord < 3 {
            return false
        }else{
            return true
        }
    }
    func showErrorMessage(title: String, message: String) {

        let errorTitle = title
        let errorMessage = message

        let ac = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
    }

}

3      

Yep, that seems about right!

4      

@onqun  

thank you

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.