2016-09-27 63 views
0

請注意,我沒有創建此函數,它是庫的一部分,尚未由其創建者更新。有人可以請教如何將函數切換到swift 3.0嗎?將函數重新格式化爲swift 3.0語法

func debounce(delay:TimeInterval, queue:DispatchQueue, action: @escaping (()->())) ->()->() { 
     lastFireTime = dispatch_time(DISPATCH_TIME_NOW,0) 
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW,dispatchDelay),queue) { 
     let now = dispatch_time(DISPATCH_TIME_NOW,0) 
     let when = dispatch_time(lastFireTime, dispatchDelay) 
     if now >= when { 
     action() 
     } 
     } 

    } 
} 
+0

從方法中刪除對象的類型,如果有效的話。請參閱http://stackoverflow.com/a/39526497/3463712 – Max

回答

0

C API

dispatch_time_t 
    dispatch_time(dispatch_time_t base, int64_t offset); 

void 
    dispatch_after(dispatch_time_t when, dispatch_queue_t queue, 
     void (^block)(void)); 
在夫特

,相同的功能是經由調度框架(進口調度),這是很好的面向對象的框架建立在C API的頂部提供。嘗試在你的Playground中的下一個片段,然後檢查當前Apple documentation瞭解更多詳情。對於初學者

import PlaygroundSupport 
PlaygroundPage.current.needsIndefiniteExecution = true 

import Foundation 
import Dispatch 

let queue = DispatchQueue(label: "my queue") 

// call the block asynchronously after some time 
print(1, "now is", Date()) 

queue.asyncAfter(deadline: .now() + .seconds(5)) { 
    print() 
    print(3, "now is", Date()) 
    PlaygroundPage.current.finishExecution() 
} 

print(2, "now is", Date()) 

很好的出發點是here

我建議你在非常壓縮形式的大量信息讀取A quick look at Grand Central Dispatch and Swift 3