慕虎7371278
方法A使用NSThread.detachNewThreadSelector,关于这种方法的好处是我们可以将对象附加到消息上。ViewController中的示例代码:override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let delay = 2.0 * Double(NSEC_PER_SEC) var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) dispatch_after(time, dispatch_get_main_queue(), { NSThread.detachNewThreadSelector(Selector("greetings:"), toTarget:self, withObject: "sunshine") })}func greetings(object: AnyObject?) { println("greetings world") println("attached object: \(object)")}控制台日志:问候世界附加对象:阳光方法B这个替代方法是较早发现的,我也在设备和模拟器上进行了测试。这个想法是使用UIControl的以下方法:func sendAction(_ action: Selector, to target: AnyObject!, forEvent event: UIEvent!)ViewController中的示例代码:override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var control: UIControl = UIControl() control.sendAction(Selector("greetings"), to: self, forEvent: nil) // Use dispatch_after to invoke this line as block if delay is intended }func greetings() { println("greetings world")}控制台日志:问候世界方法CNSTimerclass func scheduledTimerWithTimeInterval(_ seconds: NSTimeInterval, target target: AnyObject!, selector aSelector: Selector, userInfo userInfo: AnyObject!, repeats repeats: Bool) -> NSTimer!