Active Filters: Libraries

Topics

#13: PromiseKit 💍

Topics

PromiseKit is a library that adds Promises support to Objective-C and Swift. Promises (sometimes referred to as "Futures") can help clean up some of the spaghetti-callback-ridden code that we've all written then never talked about again.

The main thing you should take away from this bite is that PromiseKit lets you turn repulsive, close to gibberesh code like this:

SynapseAPI.loadVideos { (videos, error) in
  if let e = error { showError(e) } else {
    var completed = 0
    var total = videos.length

    for video in videos {
      PiedPiperAPI.loadVideo { loadedVideo in
        completed++
        if completed == total {
          // finally done, UGH SRSLY
        }
      }
    }
  }
}

...into much more readable, and composable code like this:

firstly {
  SynapseAPI.loadVideos()
}.then { videos in
  // `join` waits for all the requests to finish, then continues
  join(videos.map { PiedPiperAPI.loadVideo($0) })
}.catch { error in
  // called if any error happens anywhere in the chain
}.finally {
  // done, much nicer
}

The power of Promises doesn't stop at network calls! PromiseKit comes with extensions Promise-ifying many of the traditionally disperate parts of Cocoa and Cocoa Touch like CLLocationManager, UIAlertView, and even sending a user off to another UIViewController, letting them choose something, then continuing on.

More info about PromiseKit can be found at git.io/promisekit

Topics

#11: Carthage 🗿

Topics

Carthage is a new way to integrate third-party libraries in your apps. One way to think of it is as a simpler alternative to CocoaPods. It’s a dependency manager distilled down to just about it’s simplest possible form.

One of the notable differences between Carthage and CocoaPods is that Carthage doesn’t require an Xcode Workspace to be created or "automagically" managed. It can be installed via a downloadable installer or with a simple brew install carthage.

Create a Cartfile, which is just a plain text file filled with lines like the ones below. Each of these tell Carthage about a library you’d like to use in your app.

github 'Alamofire/Alamofire'
github 'Hearst-DD/ObjectMapper'
github 'Haneke/HanekeSwift'

Then, simply run carthage update. At which point Carthage will go out and download and build all the libraries you asked for.

Note: Similar to CocoaPods’ pod install, you’ll also need to run carthage update anytime you change or update your Cartfile.

Where things begin to feel different from CocoaPods is that the next step is to manually drag and drop the frameworks Carthage built when you ran carthage update into the Linked Frameworks and Libraries of your Xcode target’s General settings panel.

Finally, import the libraries in your code:

import Alamofire
import ObjectMapper
import Haneke

More info about Carthage can be found at git.io/carthage

Topics

#3: Timepiece 🕙

Topics

Timepiece is a library from Naoto Kaneko that makes working with NSDate objects a bit more intuitive.

let now = NSDate()
let theForceAwakens = now + 218.days + 2.hours + 34.minutes

It also aids in quickly parsing dates from or rendering dates to a String:

let judgementDay = "1997-08-29".dateFromFormat("yyyy-MM-dd")
1.day.later.stringFromFormat("EEEE") // "Thursday"

Comparisons are simplified as well:

if (birthday > 40.years.ago) {
  midLifeCrisis()
}

How to Use Timepiece

In our Podfile:

platform :ios, '8.0'

pod 'Timepiece'

In any Swift file:

import Timepiece

More info about Timepiece can be found at git.io/timepiece

Page 10 of 10