Skip to content

Instantly share code, notes, and snippets.

@nbasham
nbasham / Array
Last active Feb 18, 2017
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.
View Array
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 Jan 29, 2016
Run a block on the main thread after a specified number of seconds.
View ThreadUtil.swift
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)
}