Skip to content

Instantly share code, notes, and snippets.

@kentliau
Forked from calebd/ArrayHelpers.swift
Last active August 29, 2015 14:15
Show Gist options
  • Save kentliau/e609ff95c7bd862b1a88 to your computer and use it in GitHub Desktop.
Save kentliau/e609ff95c7bd862b1a88 to your computer and use it in GitHub Desktop.
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
if isEmpty {
return nil
}
let index = count - 1
return self[index]
}
func head() -> Element? {
return first()
}
func tail() -> [Element]? {
if isEmpty || count == 1 {
return nil
}
let range: Range<Int> = Range(start: 1, end: count)
let slice = self[range]
return Array(slice)
}
}
// Nice helper function for dispatch_after
func dispatch_after_delay(delay: NSTimeInterval, queue: dispatch_queue_t, block: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, queue, block)
}
// Or you can do it the old way
let offset = 2.0
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(offset * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
// Do something
})
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
delay(0.4) {
// do stuff
}
import UIKit
extension NSNotification {
func cmd_beginKeyboardAnimation() {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(cmd_keyboardAnimationDuration())
UIView.setAnimationCurve(cmd_keyboardAnimationCurve())
}
func cmd_commitKeyboardAnimation() {
UIView.commitAnimations()
}
func cmd_performAnimationsWithKeyboardAnimation(block: (Void) -> Void) {
cmd_beginKeyboardAnimation()
block()
cmd_commitKeyboardAnimation()
}
func cmd_keyboardFrameInView(view: UIView) -> CGRect {
let keyboardFrame = cmd_keyboardFrameEnd()
return view.convertRect(keyboardFrame, fromView: view);
}
func cmd_keyboardFrameEnd() -> CGRect {
return userInfo[UIKeyboardFrameEndUserInfoKey].CGRectValue()
}
func cmd_keyboardAnimationDuration() -> NSTimeInterval {
return userInfo[UIKeyboardAnimationDurationUserInfoKey] as NSTimeInterval
}
func cmd_keyboardAnimationCurve() -> UIViewAnimationCurve {
var curve = UIViewAnimationCurve.Linear
userInfo[UIKeyboardAnimationCurveUserInfoKey].getValue(&curve)
return curve
}
}
import Foundation
extension NSDate: Comparable {
}
func <=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs == rhs || lhs < rhs
}
func >=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs == rhs || lhs > rhs
}
func >(lhs: NSDate, rhs: NSDate) -> Bool {
let result = lhs.compare(rhs)
return result == NSComparisonResult.OrderedDescending
}
/**
This method is not required by the Comparable protocol but Xcode 6 beta 3
would not compile without it.
*/
func <(lhs: NSDate, rhs: NSDate) -> Bool {
let result = lhs.compare(rhs)
return result == NSComparisonResult.OrderedAscending
}
func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.isEqualToDate(rhs)
}
/**
Operators for incrementing dates.
*/
func +(date: NSDate, interval: NSTimeInterval) -> NSDate {
return date.dateByAddingTimeInterval(interval)
}
@assignment func +=(inout date: NSDate, interval: NSTimeInterval) {
date = date + interval
}
func -(date: NSDate, interval: NSTimeInterval) -> NSDate {
return date.dateByAddingTimeInterval(-interval)
}
@assignment func -=(inout date: NSDate, interval: NSTimeInterval) {
date = date - interval
}
class func sharedClient() -> CanaryClient {
struct Static {
static var instance: CanaryClient?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token, {
Static.instance = CanaryClient()
})
return Static.instance!
}
@infix func * (template: String, count: Int) -> String {
var string: String = ""
for i in 0..count {
string += template
}
return string
}
// You can make aliases for closure types
typealias AccountStoreMultipleAccountsCompletion = ([ACAccount]?, NSError?) -> ()
typealias AccountStoreSingleAccountCompletion = (ACAccount?, NSError?) -> ()
// Or embed them in a type
class AccountStore {
typealias MultipleAccountsCompletion = ([ACAccount]?, NSError?) -> ()
typealias SingleAccountCompletion = (ACAccount?, NSError?) -> ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment