BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Confusion about refactoring, project 5, challenge 2

Forums > 100 Days of Swift


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() {

        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"]


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

    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)

        let submitAction = UIAlertAction(title: "Submit", style: .default) {
            [weak self, weak ac] _ in
            guard let answer = ac?.textFields?[0].text else { return}
        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)
                    } 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"

                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
            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)



Yep, that seems about right!



thank you


Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn 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.