为什么performSelector不存在于swift中

显然以下是不能在迅速:

[self performSelector:@selector(onFlip) withObject:nil afterDelay:0.3]; 

为什么这样的情况下,如果仍然存在:

 NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: singleTapSelector, object: nil) 

不是nsobject.cancel与performSelector? 为什么有取消function,但不是执行function?

使用

 NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: "onFlip", userInfo: nil, repeats: false) 

代替。 不知道为什么另一个不可用。

我们可以很容易地在Swift 1.x中解开这些方法! 好极了!

这些来自perfromSelector系列的方法实际上存在于Swift中,但在Swift 2.0之前,它们被人为地隐藏(我认为是通过devise)。 他们可以使用swizzling和一些Objective-C运行库在Swift中正确的使用。

只需添加这个扩展文件NSObject+PerformSelector.swift并用🚀符号前缀所有调用。

在你的情况下代码:

 [self performSelector:@selector(onFlip) withObject:nil afterDelay:0.3]; 

可以像这样在Swift中重写:

 self.🚀performSelector("onFlip", withObject: nil, afterDelay: 0.3) 

使用这个“外延类”(class extension): NSObject+PerformSelector.swift

 import Foundation private var dispatchOnceToken: dispatch_once_t = 0 private var selectors: [Selector] = [ "performSelector:", "performSelector:withObject:", "performSelector:withObject:withObject:", "performSelector:withObject:afterDelay:inModes:", "performSelector:withObject:afterDelay:", ] private func swizzle() { dispatch_once(&dispatchOnceToken) { for selector: Selector in selectors { let 🚀selector = Selector("🚀\(selector)") let method = class_getInstanceMethod(NSObject.self, selector) class_replaceMethod( NSObject.self, 🚀selector, method_getImplementation(method), method_getTypeEncoding(method) ) } } } extension NSObject { func 🚀performSelector(selector: Selector) -> AnyObject? { swizzle() return self.🚀performSelector(selector) } func 🚀performSelector(selector: Selector, withObject object: AnyObject?) -> AnyObject? { swizzle() return self.🚀performSelector(selector, withObject: object) } func 🚀performSelector(selector: Selector, withObject object1: AnyObject?, withObject object2: AnyObject?) -> AnyObject? { swizzle() return self.🚀performSelector(selector, withObject: object1, withObject: object2) } func 🚀performSelector(selector: Selector, withObject object: AnyObject?, afterDelay delay: NSTimeInterval, inModes modes: [AnyObject?]?) { swizzle() self.🚀performSelector(selector, withObject: object, afterDelay: delay, inModes: modes) } func 🚀performSelector(selector: Selector, withObject object: AnyObject?, afterDelay delay: NSTimeInterval) { swizzle() self.🚀performSelector(selector, withObject: object, afterDelay: delay) } } 
 func performFlipOnDelay() { let delayInSeconds = 1.0 let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC))) dispatch_after(delay, dispatch_get_main_queue()) { onFlip(); } } 

关于答案的另一回合是使用GCD为要执行的动作添加延迟时间

从Thomas Kilian得到的答案更进一步,主要是为了增强对客户端的可读性,我定义了一个swift实用程序TimerUtil.swift,为NSTimer定义了这个特定用例的外观:

 import Foundation /** * Convenience function to make a single timer scheduling more readable. */ public func invoke( selector:Selector, on target:AnyObject, afterDelay delay:NSTimeInterval ) { NSTimer.scheduledTimerWithTimeInterval( delay, target: target, selector: selector, userInfo: nil, repeats: false ) } 

那么你的代码可以简单地调用:

 invoke( "onFlip", on:self, afterDelay:0.3 ) 

其中或多或less类似于performSelector:withObject:afterDelay