Gcd encapsulation (swift) and gcd encapsulation swift
Gcd encapsulation (swift) by Wu xueying
Overridefunc viewDidLoad (){
Delay (2 ){
Println ("2 s later ")
}
Let task = delay (5 ){
Println ("call ")
}
Cancel (task)
}
Typealias Task = (cancel: Bool)-> ()
Func delay (time: NSTimeInterval, task :()-> ()-> Task? {
Func dispatch_later (block :()-> ()){
Dispatch_after (
Dispatch_time (
DISPATCH_TIME_NOW,
Int64 (time * Double (NSEC_PER_SEC ))),
Dispatch_get_main_queue (),
Block)
}
Var closure: dispatch_block_t? = Task
Var result: Task?
Let delayedClosure: Task = {
Cancel in
Iflet internalClosure = closure {
If (cancel = false ){
Dispatch_async (dispatch_get_main_queue (), internalClosure );
}
}
Closure = nil
Result = nil
}
Result = delayedClosure
Dispatch_later {
Iflet delayedClosure = result {
DelayedClosure (cancel: false)
}
}
Return result;
}
Func cancel (task: Task ?) {
Task? (Cancel: true )}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.