diff --git a/CleanroomBridging.xcodeproj/project.pbxproj b/CleanroomBridging.xcodeproj/project.pbxproj index c1f181c..a1ca205 100644 --- a/CleanroomBridging.xcodeproj/project.pbxproj +++ b/CleanroomBridging.xcodeproj/project.pbxproj @@ -10,6 +10,10 @@ 3B00E5681BA3782D0011486D /* CleanroomBridging.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B00E55D1BA3782D0011486D /* CleanroomBridging.framework */; }; 3B00E5861BA379830011486D /* CleanroomBridging.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B00E57C1BA379830011486D /* CleanroomBridging.framework */; }; 3B00E5A41BA37A420011486D /* CleanroomBridging.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B00E59A1BA37A420011486D /* CleanroomBridging.framework */; }; + 3B26C2441BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; }; + 3B26C2451BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; }; + 3B26C2461BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; }; + 3B26C2471BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */; }; 3BFFEB7C1BAA140A0042FFF5 /* AssociatedObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BFFEB761BAA140A0042FFF5 /* AssociatedObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3BFFEB7D1BAA140A0042FFF5 /* AssociatedObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BFFEB761BAA140A0042FFF5 /* AssociatedObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; 3BFFEB7E1BAA140A0042FFF5 /* AssociatedObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 3BFFEB761BAA140A0042FFF5 /* AssociatedObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -75,6 +79,7 @@ 3B00E5851BA379830011486D /* CleanroomBridgingUnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CleanroomBridgingUnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 3B00E59A1BA37A420011486D /* CleanroomBridging.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CleanroomBridging.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 3B00E5A31BA37A420011486D /* CleanroomBridgingUnitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CleanroomBridgingUnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTimerExtension.swift; sourceTree = ""; }; 3B38DBD01BA76428000986DF /* Debug-iOS.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Debug-iOS.xcconfig"; sourceTree = ""; }; 3B38DBD11BA76428000986DF /* Debug-OSX.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Debug-OSX.xcconfig"; sourceTree = ""; }; 3B38DBD21BA76428000986DF /* Debug-tvOS.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Debug-tvOS.xcconfig"; sourceTree = ""; }; @@ -231,6 +236,7 @@ 3BFFEB781BAA140A0042FFF5 /* Exception.h */, 3BFFEB791BAA140A0042FFF5 /* Exception.m */, 3BFFEB9D1BAA156B0042FFF5 /* NotificationWatcher.swift */, + 3B26C2431BFD2A030026F0A6 /* NSTimerExtension.swift */, 3BFFEB7B1BAA140A0042FFF5 /* TargetAction.swift */, ); path = Code; @@ -527,6 +533,7 @@ 3BFFEB801BAA140A0042FFF5 /* AssociatedObject.m in Sources */, 3BFFEB9E1BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */, 3BFFEB901BAA140A0042FFF5 /* TargetAction.swift in Sources */, + 3B26C2441BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */, 3BFFEB881BAA140A0042FFF5 /* Exception.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -547,6 +554,7 @@ 3BFFEB811BAA140A0042FFF5 /* AssociatedObject.m in Sources */, 3BFFEB9F1BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */, 3BFFEB911BAA140A0042FFF5 /* TargetAction.swift in Sources */, + 3B26C2451BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */, 3BFFEB891BAA140A0042FFF5 /* Exception.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -567,6 +575,7 @@ 3BFFEB821BAA140A0042FFF5 /* AssociatedObject.m in Sources */, 3BFFEBA01BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */, 3BFFEB921BAA140A0042FFF5 /* TargetAction.swift in Sources */, + 3B26C2461BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */, 3BFFEB8A1BAA140A0042FFF5 /* Exception.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -587,6 +596,7 @@ 3BFFEB831BAA140A0042FFF5 /* AssociatedObject.m in Sources */, 3BFFEBA11BAA156B0042FFF5 /* NotificationWatcher.swift in Sources */, 3BFFEB931BAA140A0042FFF5 /* TargetAction.swift in Sources */, + 3B26C2471BFD2A030026F0A6 /* NSTimerExtension.swift in Sources */, 3BFFEB8B1BAA140A0042FFF5 /* Exception.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Code/NSTimerExtension.swift b/Code/NSTimerExtension.swift new file mode 100644 index 0000000..ee8ab40 --- /dev/null +++ b/Code/NSTimerExtension.swift @@ -0,0 +1,66 @@ +// +// NSTimerExtension.swift +// Cleanroom Project +// +// Created by Evan Maloney on 10/1/15. +// Copyright © 2015 Gilt Groupe. All rights reserved. +// + +import Foundation + +extension NSTimer +{ + public typealias TimerFiredCallback = (NSTimer) -> Void + + public class func scheduledTimerWithTimeInterval(interval: NSTimeInterval, userInfo: AnyObject? = nil, repeats: Bool = false, callback: TimerFiredCallback) + -> NSTimer + { + let ta = TargetAction() { (obj: AnyObject?) in + callback(obj as! NSTimer) + } + + let timer = NSTimer(timeInterval: interval, target: ta, selector: ta.action, userInfo: userInfo, repeats: repeats) + + NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) + + return timer + } + + public class func scheduledTimerWithFireDate(fireDate: NSDate, callback: TimerFiredCallback) + -> NSTimer + { + let timer = NSTimer(fireDate: fireDate, callback: callback) + + NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) + + return timer + } + + public class func scheduledTimerWithFireDate(fireDate: NSDate, repeatInterval: NSTimeInterval, callback: TimerFiredCallback) + -> NSTimer + { + let timer = NSTimer(fireDate: fireDate, repeatInterval: repeatInterval, callback: callback) + + NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes) + + return timer + } + + public convenience init(fireDate: NSDate, callback: TimerFiredCallback) + { + let ta = TargetAction() { (obj: AnyObject?) in + callback(obj as! NSTimer) + } + + self.init(fireDate: fireDate, interval: 0.0, target: ta, selector: ta.action, userInfo: nil, repeats: false) + } + + public convenience init(fireDate: NSDate, repeatInterval: NSTimeInterval, callback: TimerFiredCallback) + { + let ta = TargetAction() { (obj: AnyObject?) in + callback(obj as! NSTimer) + } + + self.init(fireDate: fireDate, interval: repeatInterval, target: ta, selector: ta.action, userInfo: nil, repeats: true) + } +} diff --git a/Tests/ExceptionTrapTests.swift b/Tests/ExceptionTrapTests.swift index 9c84193..780a238 100644 --- a/Tests/ExceptionTrapTests.swift +++ b/Tests/ExceptionTrapTests.swift @@ -3,7 +3,7 @@ // Cleanroom // // Created by Evan Maloney on 1/2/15. -// Copyright (c) 2015 Gilt Groupe. All rights reserved. +// Copyright © 2015 Gilt Groupe. All rights reserved. // import XCTest diff --git a/Tests/TargetActionTests.swift b/Tests/TargetActionTests.swift index c6200cd..767503a 100644 --- a/Tests/TargetActionTests.swift +++ b/Tests/TargetActionTests.swift @@ -3,7 +3,7 @@ // Cleanroom Project // // Created by Evan Maloney on 3/31/15. -// Copyright (c) 2015 Gilt Groupe. All rights reserved. +// Copyright © 2015 Gilt Groupe. All rights reserved. // import Foundation