Helpers.swift 1.27 KB
Newer Older
Aral Balkan's avatar
Aral Balkan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
//
//  File.swift
//  Heartbeat
//
//  Created by Aral Balkan on 25/09/2014.
//  Copyright (c) 2014 ind.ie. All rights reserved.
//

import Foundation

//
// Helpers
//

func delay(delay:Double, closure:()->())
{
    //
    // Run the passed closure after a given number of seconds.
    //
    // Courtesy: http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift
    //
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51


//
// Coverted to Swift from Cameron Lowell Palmer’s Obj-C category
// (http://stackoverflow.com/a/23606211/253485)
//
extension String {
    
    func stringByStrippingCharactersInSet(set:NSCharacterSet) -> String
    {
        return (self.componentsSeparatedByCharactersInSet(set) as NSArray).componentsJoinedByString("")
    }
    
    func stringByCollapsingWhitespace() -> String
    {
        var components:NSArray = self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
        let predicate = NSPredicate(format: "self <> ''", argumentArray: nil)
        components = components.filteredArrayUsingPredicate(predicate)
        
        return components.componentsJoinedByString(" ")
    }
}