Day 14: Kind of back on track and yet another tip calculator app…

October 13, 2015 Will Lam

So this past weekend was a bit worrisome in terms of keeping up with the challenge. Without getting into the details, there was some personal stuff that I had to deal with that kind of knocked the wind out of me and then followed up with Canadian Thanksgiving, that sort of doused a bit of the momentum.

BUT.. not to worry, I’m still back at it. My mindset is like a marathon, and my effort is still very much a sprint.

Excuses aside, I’ve been struggling with wrapping my head with optionals. It wasn’t until I hand traced the code that I was able to get a much better understanding over what was going on that it really sunk in.

Also, reading this post on Growth Mindset allayed any concerns about being too dumb to learn this stuff. At the end of the day, we’re just giving our computers instructions to do our bidding. Every new thing we learn should be applied immediately and done the hard way, so that we can break down the supposed complexity that we face.

Below you can see two different approaches in terms of unwrapping optionals with If-Statements and Implicitly Unwrapping optionals through the two IBAction buttons:

Tip calculator with optionals

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var billTextField: UITextField!
  @IBOutlet weak var tipLabel: UILabel!
  @IBOutlet weak var totalLabel: UILabel!
  
  var tipValue: Double? = 20
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  }

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

  @IBAction func convertButtonTapped(sender: UIButton) {
    billTextField.resignFirstResponder()
    if billTextField.text != nil {
      
      if billTextField.text! != "" {
        if tipValue != nil {
          if tipValue! != 0 {
            tipLabel.text = "\(tipValue!)"
            
            let totalWithTip = tipValue! + Double(billTextField.text!)!
            totalLabel.text = "\(totalWithTip)"
          }
          else {
            tipLabel.text = "No tip!"
            totalLabel.text = "\(billTextField.text!)"
          }
          
        } else {
          tipLabel.text = "Tip is nil!"
          totalLabel.text = "\(billTextField.text!)"
        }
      }
      
    }
  }

  @IBAction func implicitlyConvertButtonTapped(sender: UIButton) {
    billTextField.resignFirstResponder()
    
    let total = Double(billTextField.text!)
    
    if let unwrappedTotal = total {
      if let tipValueToUse = tipValue {
        tipLabel.text = "\(tipValueToUse)"
        let amountToPay = unwrappedTotal + tipValueToUse
        totalLabel.text = "\(amountToPay)"
      }
      else {
        tipLabel.text = "Tip is nil"
        totalLabel.text = "\(unwrappedTotal)"
      }
    }
    else {
      print("Optional was nil!")
    }
}


}

Previous Article
Day 15: No Sleep + UISwitch Fun
Day 15: No Sleep + UISwitch Fun

Had an amazing morning with Morning Reset by Camp Reset and dancing my butt off from 7:30 to 9:30 am after ...

Next Article
Day 13: Lucky number 13.. not really an app but more an update

So today was a bit of a slog because I had to get some stuff back for a review I was writing for Staples. Y...