Skip to content

Instantly share code, notes, and snippets.

@nbasham
nbasham / Array
Last active February 18, 2017 00:59
Swift extension of Array. Adds every function to test if all elements meet a criteria and filterReturnIndexes that filters and returns the indexes of elements that meet criteria.
import UIKit
public extension Sequence {
// return true if all elements return true
func every<T>(predicate:(T) -> Bool) -> Bool {
for item in self {
if !predicate(item as! T) {
return false
}
@nbasham
nbasham / ThreadUtil.swift
Last active January 29, 2016 23:21
Run a block on the main thread after a specified number of seconds.
import Foundation
public class ThreadUtil : NSObject {
// Run a block on the main thread after a specified number of seconds, thanks raywenderlich.com for splaining it so well
public static func runOnMainThreadAfterDelay(delaySeconds: Double, closure: () -> ()) {
let startTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delaySeconds * Double(NSEC_PER_SEC)))
dispatch_after(startTime, dispatch_get_main_queue(), closure)
}