Quantifire

Growth Marketing, Learning to Code & Bootstrapping Businesses

  • GitHub
  • Google+
  • Instagram
  • LinkedIn
  • Twitter
  • Home
  • Learning
  • Programming
  • About
  • February 16, 2019

Day 12: Movie Database app with Tuples

October 7, 2015 by Will Lam Leave a Comment

Okay, while it doesn’t hook into IMDB, Flixster, APIs or anything cool like that (yet), today I went through Bitfountain’s foundations course and completed something using tuples to draw the data from. It does only one thing and has data for a movie, but gives me an introductory idea of how to pull data from a tuple and display it in a view.

Check it out:

movieDatabase app using tuples

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var titleLabel: UILabel!
  @IBOutlet weak var directorLabel: UILabel!
  @IBOutlet weak var writerLabel: UILabel!
  @IBOutlet weak var studioLabel: UILabel!
  @IBOutlet weak var ratedLabel: UILabel!
  @IBOutlet weak var posterImageView: UIImageView!
  
  let dataRecord = (title: "Midnight Run", director: "MartinBrest", writer: "George Gallo", studio: "Universal Studios", rated: "R", year: "1988")
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    titleLabel.text = ""
    directorLabel.text = ""
    writerLabel.text = ""
    studioLabel.text = ""
    ratedLabel.text = ""
    
    posterImageView.hidden = true
  }

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

  @IBAction func retrieveDataButtonTapped(sender: UIButton) {
    
    posterImageView.hidden = false
    titleLabel.text = "\(dataRecord.title) \(dataRecord.year)"
    directorLabel.text = dataRecord.director
    writerLabel.text = dataRecord.writer
    studioLabel.text = dataRecord.studio
    ratedLabel.text = dataRecord.rated
  }

}

Here’s today’s bonus nerdWOD:

Inspired by my alma gym mater’s WOD from this past Sunday:

AMRAP 9 mins:

15 kettlebell swings @ 24 kg

12 kettlebell lunges @ 24 kg

15 kettlebell swings

9 kettlebell goblet squats

… I finished 2 rounds + 2 kettlebell swings.

Filed Under: Challenges, Learning, Programming, Swift

Day 11: Struggling to find new app ideas, but some UIPickerView fun

October 6, 2015 by Will Lam Leave a Comment

Okay, to be honest, I’m kind of limited to the stuff I can really build so I’m reaching out to see what other things I can implement that can augment my learning.

I’m going to have to re-evaluate my day and figure out the optimal times of learning and executing. Right now, the pace at which I’m learning is much slower than which I can execute at. The stuff that I’m going through on Bitfountain is pretty dry.. optionals, implicit unwrapped optionals, blah blah blah. It’s important stuff, but it isn’t engaging me like the other stuff I encountered.. but I gotta power through this. I’m giving myself until the end of the week to finish up the Foundations course.

Much like the last few days, I’ve had to reach outside of Bitfountain to find new things I can learn and build in less than a day. Today after scrolling the Objects library tab, I happened upon the UIPickerView. Because I didn’t get to play around with it yet, I went ahead and googled something up.

While I could’ve went a bit further in terms of having the background image to change upon toggling the picker, like choosing seasons for instance.. I’m going to make it happen at a future date.

Here’s what it looks like and below is the code:

UIPickerView gif

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

  @IBOutlet weak var picker: UIPickerView!
  
  var pickerData: [[String]] = [[String]]()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    self.picker.delegate = self
    self.picker.dataSource = self
    
    pickerData = [["1", "2", "3", "4"],
      ["a", "b", "c", "d"],
      ["!", "#", "$", "%"],
      ["w", "x", "y", "z"]]
    
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 4
  }
  
  func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerData.count
  }
  
  func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerData[component][row]
  }

}

Filed Under: Challenges, Learning, Programming, Swift

Day 10: Simple Timer App

October 5, 2015 by Will Lam Leave a Comment

Okay, so today was a bit weird as I was working out of a new space at CSI Annex, which threw me off, but here I still am learning, so that’s not all bad right?

Anyway, today I stumbled upon a two different tutorials teaching the “proper” use of NSTimer. The basis for this app was from Ravi Shankar’s blog post, “Simple Stopwatch App in Swift“.  However, upon more investigative googling, I happened upon another post which felt a bit more “cleaner” in terms of how they presented the tutorial content

It was a bit of a roundabout way in terms of building the app. I don’t know why he decided to delete the viewcontroller, only to create another one in order to make all the connections. It doesn’t make sense to me.

However, on iOSBlog, their timer app made much more sense to me.

In any case, my general understanding is increasing a bit, because I’ve always wanted to do a timer related app, and this is the first among many others that will be using the NSTimer class.

This app stretched my understanding a bit more in terms of how to use the Foundation frameworks reference documentation, which is a first for me.  While I’m kind of understanding how to update the labels and use the IBAction functions (the buttons), there was the updateTime function that I pretty much copied and pasted in order to understand what moving piece did.

Here’s how the app looks and below that is the code:

simple timer app in swift

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var timerLabel: UILabel!
  
  @IBOutlet weak var startButton: UIButton!
  
  @IBOutlet weak var stopButton: UIButton!
  
  var startTime = NSTimeInterval()
  
  var timer: NSTimer = NSTimer()
  
  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 startButtonPressed(sender: UIButton) {
    if !timer.valid {
      let aSelector : Selector = "updateTime"
      timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
      startTime = NSDate.timeIntervalSinceReferenceDate()

    }
  }
  
  @IBAction func stopButtonPressed(sender: UIButton) {
    timer.invalidate()
  }
  
  func updateTime() {
    let currentTime = NSDate.timeIntervalSinceReferenceDate()
    
    //Find the difference between current time and start time.
    var elapsedTime: NSTimeInterval = currentTime - startTime
    
    //calculate the minutes in elapsed time.
    let minutes = UInt8(elapsedTime / 60.0)
    elapsedTime -= (NSTimeInterval(minutes) * 60)
    
    //calculate the seconds in elapsed time.
    let seconds = UInt8(elapsedTime)
    elapsedTime -= NSTimeInterval(seconds)
    
    //find out the fraction of milliseconds to be displayed.
    let fraction = UInt8(elapsedTime * 100)
    
    //add the leading zero for minutes, seconds and millseconds and store them as string constants
    
    let strMinutes = String(format: "%02d", minutes)
    let strSeconds = String(format: "%02d", seconds)
    let strFraction = String(format: "%02d", fraction)
    
    //concatenate minuets, seconds and milliseconds as assign it to the UILabel
    timerLabel.text = "\(strMinutes):\(strSeconds):\(strFraction)"
  }
  

}

Filed Under: Challenges, Learning, Programming, Swift

Day 9: Tip Calculator App

October 3, 2015 by Will Lam Leave a Comment

Great news today! But I also have some bad news. I’ll start with the bad first.

I broke my own rule by not creating an app on a weekday as I’m still a bit short on some app ideas because my understanding is still very much beginner. I hustled and tried to go through Ray Wenderlich’s Swift tutorial (on the subway, no less!) and only got  through half of it.  I found it surprisingly straightforward, but only because I had done a good amount of work and studying via Bitfountain’s course.

But I had good reason! I was busy prepping for the Lighthouse Labs technical interviews, which I passed, with a minor hiccup, which was kinda funny actually. Anyway.

The app below is a simple tip calculator app. It introduced a UI element that I was unfamiliar with, which was the slider, implementing a model in the TipCalculator.swift file, and a whole bunch of code that involved math that I wasn’t too familiar with – a copy and paste job in terms of the code, but good to see how other instructors are teaching how to use storyboards and connecting IB outlets and actions from code to storyboard, versus from storyboard to code as Bitfountain teaches.

This is what the apps look like:

Tip calculator app Swift

Here’s the view controller:

import UIKit

class ViewController: UIViewController {
  
  @IBOutlet var totalTextField : UITextField!
  @IBOutlet var taxPctSlider : UISlider!
  @IBOutlet var taxPctLabel : UILabel!
  @IBOutlet var resultsTextView : UITextView!
  let tipCalc = TipCalculatorModel(total: 33.25, taxPct: 0.06)
 
  func refreshUI() {
    // 1
    totalTextField.text = String(format: "%0.2f", tipCalc.total)
    // 2
    taxPctSlider.value = Float(tipCalc.taxPct) * 100.0
    // 3
    taxPctLabel.text = "Tax Percentage (\(Int(taxPctSlider.value))%)"
    // 4
    resultsTextView.text = ""
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    refreshUI()
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  @IBAction func calculateTapped(sender : AnyObject) {
    // 1
    tipCalc.total = Double((totalTextField.text! as NSString).doubleValue)
    // 2
    let possibleTips = tipCalc.returnPossibleTips()
    var results = ""
    // 3
    var keys = Array(possibleTips.keys)
    keys.sortInPlace()
    for tipPct in keys {
      let tipValue = possibleTips[tipPct]!
      let prettyTipValue = String(format:"%.2f", tipValue)
      results += "\(tipPct)%: \(prettyTipValue)\n"
    }
    // 5
    resultsTextView.text = results
  }

  @IBAction func taxPercentageChanged(sender : AnyObject) {
    tipCalc.taxPct = Double(taxPctSlider.value) / 100.0
    refreshUI()
  }
  
  @IBAction func viewTapped(sender : AnyObject) {
    totalTextField.resignFirstResponder()
  }
  
}

Here’s the model:

import Foundation

class TipCalculatorModel {
  
  var total: Double
  var taxPct: Double
  var subtotal: Double {
    get {
      return total / (taxPct + 1)
    }
  }
  
  init(total: Double, taxPct: Double) {
    self.total = total
    self.taxPct = taxPct
  }
  
  func calcTipWithTipPct(tipPct: Double) -> Double {
    return subtotal * tipPct
  }
  
  func returnPossibleTips() -> [Int: Double] {
    
    let possibleTipsInferred = [0.15, 0.18, 0.20]
    
    var retval = [Int: Double]()
    for possibleTip in possibleTipsInferred {
      let intPct = Int(possibleTip*100)
      retval[intPct] = calcTipWithTipPct(possibleTip)
    }
    return retval
    
  }
  
}

Filed Under: Learning, Programming, Swift, Swift

Day 8: Ghetto Flashlight App

October 1, 2015 by Will Lam Leave a Comment

Today I learned about Tuples, Optionals, use of nil, and Optional Binding. I didn’t find any applicable and immediate use off the top of my head. Instead, I created this using some of the stuff I learned from the previous day involving booleans and some simple control flow, along creating the elements/labels/buttons via Storyboard. This was all done in the viewController file.

Here’s what it looks like in the simulator:

 

ghetto flashlight appYou can check out the code below:

import UIKit

class ViewController: UIViewController {

  var buttonPower: Bool = false
  
  @IBOutlet weak var ghettoFlashLightLabel: UILabel!
  @IBOutlet weak var onButton: UIButton!
  @IBOutlet weak var offButton: UIButton!
  
  
  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 onButtonPressed(sender: UIButton) {
    buttonPower = true
    
    if buttonPower == true {
      view.backgroundColor = UIColor.whiteColor()
      ghettoFlashLightLabel.textColor = UIColor.blackColor()
    }
    
    
  }
  
  
  @IBAction func offButtonPressed(sender: UIButton) {
    buttonPower = false
    
    if buttonPower == false {
      view.backgroundColor = UIColor.blackColor()
      ghettoFlashLightLabel.textColor = UIColor.whiteColor()
    }
  }

}

Filed Under: Learning, Programming, Swift

  • « Previous Page
  • 1
  • …
  • 3
  • 4
  • 5
  • 6
  • 7
  • …
  • 27
  • Next Page »

Copyright © 2019 · Parallax Pro Theme on Genesis Framework · WordPress · Log in