Skip to content

Instantly share code, notes, and snippets.

@zhaar
Created July 13, 2016 18:08
Show Gist options
  • Save zhaar/5b4a48e0ce80311a4e073abc029411a8 to your computer and use it in GitHub Desktop.
Save zhaar/5b4a48e0ce80311a4e073abc029411a8 to your computer and use it in GitHub Desktop.
//You can put those functions in an external file. Call it for example "Utilities.swift"
//Random number between ranges
func random(min min: Int, max: Int) -> Int {
return Int(arc4random_uniform(UInt32(max - min))) + min
}
func random(range: Range<Int>) -> Int {
return random(min: range.startIndex, max: range.endIndex)
}
//You can call it like that:
random(-10...10)
random(0...1)
random(-10...10)
random(-10...10)
//delay a function after a certain amount of time
func delay(delay: Double, fn:() -> ()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), fn)
}
//You can call it like so where the 0.5 is the waiting time in seconds
delay(0.5) {
print("hello")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment