Skip to content

Instantly share code, notes, and snippets.

@Appletone
Last active August 29, 2015 14:04
Show Gist options
  • Save Appletone/d8d2cbd91c5dbe05861c to your computer and use it in GitHub Desktop.
Save Appletone/d8d2cbd91c5dbe05861c to your computer and use it in GitHub Desktop.
Swift postfix function like Linux Shell background operator
// define Swift postfix function like Linux Shell background operator
operator postfix & {}
@postfix func & (backgroundClosure: () -> ()) {
dispatch_async(_queue) {
backgroundClosure()
}
}
// testing log function
func log(message: String)
{
let main = NSThread.currentThread().isMainThread
let name = main ? "[main]" : "[back]"
println("\(name) \(message)")
}
// let's try
var a_process = { log("my background thread") }
a_process&
// it will log => [back] my background thread
// inspired by http://ijoshsmith.com/2014/07/05/custom-threading-operator-in-swift/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment