2525import Foundation
2626
2727extension NSTimer {
28+
29+ // MARK: Create a timer without scheduling
30+
31+ /// Create a timer that will call `block` once after the specified time.
32+ ///
33+ /// - Note: The timer won't fire until it's scheduled on the run loop.
34+ /// Use `NSTimer.after` to create and schedule a timer in one step.
35+ /// - Note: If you support iOS 8 or older, or OS X 10.11 or older, use `NSTimer.new(after:)`
36+ /// instead of this initializer.
37+
38+ @available ( iOS 9 , OSX 10 . 11 , watchOS 2 , tvOS 9 , * )
39+ public convenience init ( after interval: NSTimeInterval , _ block: ( ) -> Void ) {
40+ let actor = Actor { _ in block ( ) }
41+ self . init ( timeInterval: interval, target: actor , selector: #selector( Actor . fire) , userInfo: nil , repeats: false )
42+ }
43+
44+ /// Create a timer that will call `block` repeatedly in specified time intervals.
45+ ///
46+ /// - Note: The timer won't fire until it's scheduled on the run loop.
47+ /// Use `NSTimer.every` to create and schedule a timer in one step.
48+ /// - Note: If you support iOS 8 or older, or OS X 10.11 or older, use `NSTimer.new(every:)`
49+ /// instead of this initializer.
50+
51+ @available ( iOS 9 , OSX 10 . 11 , watchOS 2 , tvOS 9 , * )
52+ public convenience init ( every interval: NSTimeInterval , _ block: ( ) -> Void ) {
53+ let actor = Actor { _ in block ( ) }
54+ self . init ( timeInterval: interval, target: actor , selector: #selector( Actor . fire) , userInfo: nil , repeats: true )
55+ }
56+
57+ /// Create a timer that will call `block` repeatedly in specified time intervals.
58+ /// (This variant also passes the timer instance to the block)
59+ ///
60+ /// - Note: The timer won't fire until it's scheduled on the run loop.
61+ /// Use `NSTimer.every` to create and schedule a timer in one step.
62+ /// - Note: If you support iOS 8 or older, or OS X 10.11 or older, use `NSTimer.new(every:)`
63+ /// instead of this initializer.
64+
65+ @available ( iOS 9 , OSX 10 . 11 , watchOS 2 , tvOS 9 , * )
66+ @nonobjc public convenience init ( every interval: NSTimeInterval , _ block: NSTimer -> Void ) {
67+ let actor = Actor ( block)
68+ self . init ( timeInterval: interval, target: actor , selector: #selector( Actor . fire) , userInfo: nil , repeats: true )
69+ }
70+
71+ // MARK: (Legacy factory methods)
72+
2873 /// Create a timer that will call `block` once after the specified time.
2974 ///
3075 /// - Note: The timer won't fire until it's scheduled on the run loop.
@@ -39,7 +84,7 @@ extension NSTimer {
3984 /// Create a timer that will call `block` repeatedly in specified time intervals.
4085 ///
4186 /// - Note: The timer won't fire until it's scheduled on the run loop.
42- /// Use `NSTimer.after ` to create and schedule a timer in one step.
87+ /// Use `NSTimer.every ` to create and schedule a timer in one step.
4388 /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
4489
4590 public class func new( every interval: NSTimeInterval , _ block: ( ) -> Void ) -> NSTimer {
@@ -50,7 +95,7 @@ extension NSTimer {
5095 /// (This variant also passes the timer instance to the block)
5196 ///
5297 /// - Note: The timer won't fire until it's scheduled on the run loop.
53- /// Use `NSTimer.after ` to create and schedule a timer in one step.
98+ /// Use `NSTimer.every ` to create and schedule a timer in one step.
5499 /// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)
55100
56101 @nonobjc public class func new( every interval: NSTimeInterval , _ block: NSTimer -> Void ) -> NSTimer {
0 commit comments