Last active
August 29, 2015 14:04
-
-
Save Appletone/d8d2cbd91c5dbe05861c to your computer and use it in GitHub Desktop.
Swift postfix function like Linux Shell background operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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