Skip to content

alexpls/APSwiftHelpers

Repository files navigation

APSwiftHelpers

CocoaPods Build Status

Common helpers for developing iOS apps in Swift. Find them helpful? Help yourself.

Deprecation notice and Swift 3 support

When I wrote these helpers Swift 3 had not yet been released. Since then, a lot of the native Swift APIs have been improved in such a way that the syntactical sugar these helpers provided is no longer necessary.

Because of this I will no longer be maintaining this library. If you have a particular use-case for these helpers and want to see them updated for Swift 3, open an issue and let me know.

Installation

  1. Add (or create) a Podfile containing this:
use_frameworks!
pod 'APSwiftHelpers'
  1. Run pod install.
  2. Done!

Usage

Dispatch

Helpers for working with Grand Central Dispatch queues.

// Perform a block on the given queue
performOn(.Main) { self.tableView.reloadData() }
performOn(.LowPriority) { self.clearCache() }

// Delay a block on the given queue - if no queue is provided .Main is assumed
delay(0.5) { self.hideSpinner() }
delay(30, .Background) { self.logActivity() }

// Queue types available:
enum QueueType {
  case Main         // The app's main queue
  case Background   // Background queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND)
  case LowPriority  // Background queue (DISPATCH_QUEUE_PRIORITY_LOW)
  case HighPriority // Background queue (DISPATCH_QUEUE_PRIORITY_HIGH)
}

NSCoding+Helpers

Extension to NSCoding to make it more Swift-y.

// Archive an NSCoding to NSData
let note = Note(title: "secret", content: "I like turtles")
let archivedData = note.archive()

// Unarchive NSData into an NSCoding object
let unarchivedNote = Note.unarchive(archivedData)
unarchivedNote.content // "I like turtles"

CATransaction+Helpers

// Run the given block with CAAnimations disabled
withCAAnimationsDisabled { self.view.resize() }

NSLocale+Helpers

// Fetch region info from NSLocale:
NSLocale.currentLocale().countryCode // String e.g.: "US"
NSLocale.currentLocale().countryName // String e.g.: "United States"

App

// Get info about the currently running app (taken from the
// main bundle's) Info.plist file
App.name
App.version
App.formattedNameAndVersion

// Returns true/false whether the app running within the simulator or not
App.inSimulator

// Returns true/false whether the app is running in DEBUG mode
App.isDebug

DebugLogging

// Print a formatted log message for a CustomDebugStringConvertible

struct Animal: CustomDebugStringConvertible {
  let name: String
  var debugDescription: String { return name }
}

let dog = Animal(name: "Woofy")
debugLog(dog, "is tired") // prints "(Woofy) is tired" to the console