Skip to content

Instantly share code, notes, and snippets.

@Chris-Gillis
Forked from calebd/ArrayHelpers.swift
Last active August 29, 2015 14:02
Show Gist options
  • Save Chris-Gillis/9fee69b9dda54796ccde to your computer and use it in GitHub Desktop.
Save Chris-Gillis/9fee69b9dda54796ccde 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 tail() -> T[] {
if self.isEmpty || self.count == 1 {
return Array()
}
return self.subArray(NSRangeFromString("{1, \(self.count - 1)"))
}
func subArray(range : NSRange) -> T[]{
var newArray = Array()
for i in range.location...range.length {
newArray += self[i]
}
return newArray
}
}
let offset = 2.0
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(offset * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
// Do something
})
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!
}
// You can make aliases for closure types
typealias AccountStoreMultipleAccountsCompletion = (ACAccount[]?, NSError?) -> ()
typealias AccountStoreSingleAccountCompletion = (ACAccount?, NSError?) -> ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment