Categories
Florida Swift Kick Tampa Bay

Share your Swift tips and tricks at this week’s Tampa iOS meetup: Wednesday, November 18!

swift tips and tricks meetup

Hey, Tampa Bay iOS developers — here’s your chance to shine! At the next Tampa iOS Meetup (Wednesday, November 18th at 7:00 p.m.), a monthly gathering run by me and my friend Angela, we’re having a “Swift Tips and Tricks” night, where we’ll take turns sharing tips an tricks that we’ve either discovered on our own or found through others while programming in Swift.

Have you ever wanted to present something at an iOS meetup, but it was on a topic or technique that could easily be covered in ten or even five minutes? Well, this meetup is your chance to be an iOS rock star, as short presentations is what it’s all about! Whether you’ve been building apps since the Objective-C days or picked up Swift a couple of weeks ago, you’ve got knowledge to share with your fellow developers, who in turn have knowledge to share with you! Join us for an evening of demos, information exchange, and that buzz that you get when you’re in a room of smart, interesting people, one of whom is you!

Me and Angela at BarCamp Tampa Bay 2015.

In order to help kick off the event, I’ll start by presenting some tips and tricks that I’ve picked up while working on my own apps, and I’m sure Angela will be doing the same. After that, it’s everyone else! We invite discussions and questions throughout the meetup, as it’s the best way to learn.

Here are the event details:

  • What: Tampa iOS meetup, a new gathering in the area that complements the Suncoast iOS Meetup and Tampa Bay Cocoaheads, both worthwhile gatherings. We want to make sure that if you can’t make one local iOS event, there’ll always be another one in the near future!
  • When: Wednesday, November 18th, from 7:00 p.m. to 9:00 p.m.
  • Where: Energy Sense Finance, located at 3825 Henderson Blvd., Suite 300 (just west of South Dale Mabry)
  • If you have a tip or trick that you’d like to present, let us know! Drop us a line in the comments section at the bottom of our Meetup page or email me at joey@joeydevilla.com. We’ll provide a projector, and if you need one, a Mac to present on.
  • We’ll have provide some snacks and drinks. No idea what they’ll be, but I’ll post details as I found out.
Categories
Florida Swift Kick

Swift programming tip: How to execute a block of code after a specified delay

burning fuse
swift kick

Sometimes, you want some code to execute after a specified delay. For me, this happens often in user interfaces; there are many cases where I want some notification or other interface element to appear and then disappear after a couple of seconds. I used to use an NSTimer to make it happen, but nowadays, I call on a simple method called delay().

Consider the simple “Magic 8-Ball” app design shown below:

magic 8-ball app

The two functional interface items are the Tap me button and a label that displays a random “yes/no/maybe” answer in response to a button tap. The button should be disabled and the answer should remain onscreen for three seconds, after which the app should revert to its initial state, with the button enabled and the answer label blank.

Here’s the action method that responds to the Touch Up Inside event on the “Tap me” button:

@IBAction func buttonClicked(sender: UIButton) { 
  tapMeButton.enabled = false
  predictionLabel.text = randomAnswer()
  delay(3.0) {
    self.tapMeButton.enabled = true
    self.predictionLabel.text = ""
  }
}

Don’t worry too much about the randomAnswer() method; it simply returns a randomly-selected string from an array of possible answers. The really interesting method is delay(), which takes two parameters:

  • A number of seconds that the system should wait before executing a block of code. In this particular case, we want a 3-second delay.
  • The block of code to be executed after the delay. In our block, we want to blank the label and enable the button.

The block of code that we’re passing to delay() is a closure, which means it will be executed outside the current ViewController object, which in turns means that we’ve got to be explicit when capturing variables in the current scope. We can’t just refer to the button and label as tapMeButton and predictionLabel, but by their fully-qualified names, self.tapMeButton and self.predictionLabel.

Here’s the code for delay():

func delay(delay: Double, closure: ()->()) { 
  dispatch_after(
    dispatch_time(
      DISPATCH_TIME_NOW,
      Int64(delay * Double(NSEC_PER_SEC))
    ),
    dispatch_get_main_queue(),
    closure
  )
}

delay() is just a wrapper for dispatch_after(), one of the functions in Grand Central Dispatch, Apple’s library for running concurrent code on multicore processors on iOS and OS X. dispatch_after() takes three parameters:

  • How long the delay should be before executing the block of code should be,
  • the queue on which the block of code should be run, and
  • the block of code to run.

We could’ve simply used dispatch_after(), but it exposes a lot of complexity that we don’t need to deal with. Matt Neuburg, the author of the O’Reilly book iOS 9 Programming Fundamentals with Swift, found that he was using dispatch_after() so often that he wrote delay() as a wrapper to simplify his code. Which would you rather read — this…

dispatch_after(
  dispatch_time(
    DISPATCH_TIME_NOW,
    Int64(3.0 * Double(NSEC_PER_SEC))
  ),
  dispatch_get_main_queue(),
  {
    self.tapMeButton.enabled = true
    self.predictionLabel.text = ""
  }
)

…or this?

delay(3.0) {
  self.tapMeButton.enabled = true
  self.predictionLabel.text = ""
}

Here’s the code for the example “Magic 8-Ball” app, which I’ve put entirely in the view controller for simplicity’s sake:

//
// ViewController.swift
//

import UIKit


class ViewController: UIViewController {

  @IBOutlet weak var tapMeButton: UIButton!
  @IBOutlet weak var predictionLabel: UILabel!
  
  let predictions = [
    "Yes.",
    "Sure thing.",
    "But of course!",
    "I'd bet on it.",
    "AWWW YISSS!",
    "No.",
    "Nuh-uh.",
    "Absolutely not!",
    "I wouldn't bet on it.",
    "HELL NO.",
    "Maybe.",
    "Possibly...",
    "Ask again later.",
    "I can't be certain.",
    "Reply hazy. Try again later."
  ]
  
  override func viewDidLoad() {
    super.viewDidLoad()
    predictionLabel.text = ""
  }

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

  @IBAction func buttonClicked(sender: UIButton) {
    
    // When the "Tap me" button tapped,
    // we want to:
    // 1. Disable the button
    // 2. Display a random magic 8-ball answer
    // 3. Wait 3 seconds, and then:
    //    a) Enable the button
    //    b) Clear the displayed answer
    
    tapMeButton.enabled = false
    predictionLabel.text = randomAnswer()
    delay(3.0) {
      self.tapMeButton.enabled = true
      self.predictionLabel.text = ""
    }
  }
  
  func randomAnswer() -> String {
    return predictions[randomIntUpToButNotIncluding(predictions.count)]
  }
  
}


// MARK: Utility functions

func delay(delay: Double, closure: ()->()) {
  
  // A handy bit of code created by Matt Neuburg, author of a lot of books including
  // iOS Programming Fundamentals with Swift (O'Reilly 2015).
  // See his reply in Stack Overflow for details:
  // http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861
  //
  // The secret sauce is Grand Central Dispatch's (GCD) dispatch_after() function.
  // Ray Wenderlich has a good tutorial on GCD at:
  // http://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1
  
  dispatch_after(
    dispatch_time(
      DISPATCH_TIME_NOW,
      Int64(delay * Double(NSEC_PER_SEC))
    ),
    dispatch_get_main_queue(),
    closure
  )
}

func randomIntUpToButNotIncluding(count: Int) -> Int {
  return Int(arc4random_uniform(UInt32(count)))
}

Resources

zip file iconYou can see this code in action by downloading the zipped project files for the demo project, DelayDemo [220K Xcode 7 / Swift 2 project and associated files, zipped].

If you’d like to learn more about coding for concurrency with Grand Central Dispatch, a good starting place is the tutorial on Ray Wenderlich’s site. It’s a two parter; here’s part 1, and here’s part 2.

I found Matt Neuburg’s delay() method in his answer to this Stack Overflow question.

Categories
Florida Tampa Bay

Tampa’s OpenHack Ybor meetup: Tonight at Brass Tap!

openhack ybor - new world

OpenHack Ybor’s August gathering at New World Brewery.
Click the photo to see it at full size.

If you’re in the Tampa area tonight and would like to get to know your fellow developers, show off your current passion project, find out what their passion projects are, and enjoy some pizza and beer (or whatever beverage you like) in a friendly, convivial atmosphere, you might want to come to tonight’s OpenHack Ybor meetup!

openhack ybor - new world 2

Another scene from the meetup at New World Brewery.

OpenHack Ybor, held once a month at one of Ybor City’s many beer-dispensing hangouts, is run by local Ruby developer Tony Winn for software developers of all stripes who want to get to know other local developers, see what they’re up to, and enjoy some free pizza. We’ve already had two OpenHack Ybor meetups — the first at the new pub at Coppertail Brewing, and the second at New World Brewery. Tonight’s meetup, which starts at 6:30, takes place at the Brass Tap in Centro Ybor.

centro ybor

Tonight’s meetup location: Brass Tap in Centro Ybor.

If you’d like to attend, RSVP on OpenHack Ybor’s meetup page (there’s no admission, the pizza is free, you’ll have to buy your own beer) so that Tony’s got an idea of how many will be there and can order pizza accordingly. It’s fun, it’s friendly, and it’s one of my go-to geek events. I’ll be there, and I hope to see you there too!

The article also appears in my personal blog, The Adventures of Accordion Guy in the 21st Century.