4
\$\begingroup\$

I am starting to build up my portfolio, so I am starting to build projects whenever an idea pops up in my head. So, I decided to build an app that helps you decide what to do. Basically how it works is that you put some different tasks to do in a textbook and it places each task in an array, after you tap on the add button. Once you tap on the decide button the app uses a random integer value to select a random index in the array, and that is how it picks a random task. I would really appreciate and comments, suggestions, and improvements.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
//Array that holds all of the users decisions
var listOfDecisions: [String] = []

//Holds what is written in the text field
var userText : String = " "

//Random number
var randomNumber : Int = 0

//UI Elements
@IBOutlet weak var textInput: UITextField!

@IBOutlet weak var youShouldLabel: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    //Makes keyboard appear when input box is tapped on
    self.textInput.delegate = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



//Makes keyboard disappear when touched outside of the keyboard
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

//Makes the keyboard disappear when the rerurn key is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textInput.resignFirstResponder()
    return true

}

//This is the add button
@IBAction func addButton(_ sender: Any) {
    appendText()


}

@IBAction func decideButton(_ sender: Any) {
    //If statement is used to make sure that the user does not proceed without entering anything
    if (listOfDecisions.isEmpty){
        // create the alert
        let alert = UIAlertController(title: "Error", message: "You did not add anything!", preferredStyle: UIAlertControllerStyle.alert)

        // add an action (button)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

        // show the alert
        self.present(alert, animated: true, completion: nil)
    }
    else {
        makeDecision()
    }
}
//Appends the text to the array
func appendText () {
    userText = textInput.text!
    listOfDecisions.append(userText)
    print(userText)
    textInput.text = " "


}


//Pick a random index in the array and updates the UI
func makeDecision () {
    randomNumber = Int (arc4random_uniform(UInt32(listOfDecisions.count)))
    youShouldLabel.text = listOfDecisions[randomNumber]
}
\$\endgroup\$
  • 4
    \$\begingroup\$ Why don't you use the app to figure out what to do to the app so that you can find more things to do to the app so that it can suggest more things to do? \$\endgroup\$ – FreezePhoenix Aug 13 '18 at 22:13
  • 1
    \$\begingroup\$ @FreezePhoenix Because AppChoiceInception will implode and destroy the universe \$\endgroup\$ – James Aug 13 '18 at 23:59
8
\$\begingroup\$

The instance variables

var userText : String = " "
var randomNumber : Int = 0

are not needed: userText is only used within func appendText(), and randomNumber is only used within func makeDecision(). Both instance variables can be replaced by local variables in the respective function.

override func didReceiveMemoryWarning()

can be removed if it does nothing but calling super.

Instead of displaying an alert if the “Decide” button is pressed while the list is still empty, better disable that button as long as the list is empty. A disabled button is a clear indication to the user that the action is currently not possible.

Similarly, you can enable the “Add” button only if the text field is non-empty, otherwise empty strings could be added to the list. This can be done with an “editingChanged” action for the text field:

@IBAction func editingChanged(_ sender: UITextField) {
    self.addButton.isEnabled = textInput.text?.trimmingCharacters(in: .whitespaces).isEmpty == false
}

Making the keyboard disappear (i.e. resigning the first responder status) if the user touches somewhere outside of the input field seems a bit unusual to me. On the other hand, I would expect the keyboard to disappear if the “Add” button is pressed (which does currently not happen).

Generally, I would always use the concrete sender type in action methods, e.g.

@IBAction func decideButton(_ sender: UIButton)

instead of

@IBAction func decideButton(_ sender: Any)

It makes no difference in your program, but can be useful to access properties of the sending UI control without the need to cast it to the concrete type.

\$\endgroup\$

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.