我在 Objective C 中有一个应用程序,我正在过渡到 Swift。在 Objective C 中,我有这样一个方法:
[self.view performSelector:@selector(someSelector) withObject:self afterDelay:0.1f];
我和 Swift 一起工作,但我不知道怎么做,我试过了:
self.view.performSelector(Selector("someSelector"), withObject: self, afterDelay: 0.1)
这是我得到的错误: 'performSelector' is unavailable: 'performSelector' methods are unavailable
我将使用什么调用来调用方法 afterDelay
?
更新
结果是这样的:
extension NSObject {
func callSelectorAsync(selector: Selector, object: AnyObject?, delay: NSTimeInterval) -> NSTimer {
let timer = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: selector, userInfo: object, repeats: false)
return timer
}
func callSelector(selector: Selector, object: AnyObject?, delay: NSTimeInterval) {
let delay = delay * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
NSThread.detachNewThreadSelector(selector, toTarget:self, withObject: object)
})
}
}