Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions Sources/SwiftyTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@

import Foundation

extension NSTimer {
extension Timer {

// MARK: Schedule timers
// MARK: Schedule timers

/// Create and schedule a timer that will call `block` once after the specified time.

public class func after(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
let timer = NSTimer.new(after: interval, block)
public class func after(_ interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
let timer = Timer.new(after: interval, block)
timer.start()
return timer
}

/// Create and schedule a timer that will call `block` repeatedly in specified time intervals.

public class func every(interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
let timer = NSTimer.new(every: interval, block)
public class func every(_ interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
let timer = Timer.new(every: interval, block)
timer.start()
return timer
}

/// Create and schedule a timer that will call `block` repeatedly in specified time intervals.
/// (This variant also passes the timer instance to the block)

@nonobjc public class func every(interval: NSTimeInterval, _ block: NSTimer -> Void) -> NSTimer {
let timer = NSTimer.new(every: interval, block)
@nonobjc public class func every(_ interval: TimeInterval, _ block: @escaping (Timer) -> Void) -> Timer {
let timer = Timer.newWithTimer(every: interval, block)
timer.start()
return timer
}

// MARK: Create timers without scheduling
// MARK: Create timers without scheduling

/// Create a timer that will call `block` once after the specified time.
///
/// - Note: The timer won't fire until it's scheduled on the run loop.
/// Use `NSTimer.after` to create and schedule a timer in one step.
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)

public class func new(after interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
public class func new(after interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, 0, 0, 0) { _ in
block()
}
Expand All @@ -72,8 +72,8 @@ extension NSTimer {
/// - Note: The timer won't fire until it's scheduled on the run loop.
/// Use `NSTimer.every` to create and schedule a timer in one step.
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)

public class func new(every interval: NSTimeInterval, _ block: () -> Void) -> NSTimer {
public class func new(every interval: TimeInterval, _ block: @escaping () -> Void) -> Timer {
return CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in
block()
}
Expand All @@ -86,46 +86,46 @@ extension NSTimer {
/// Use `NSTimer.every` to create and schedule a timer in one step.
/// - Note: The `new` class function is a workaround for a crashing bug when using convenience initializers (rdar://18720947)

@nonobjc public class func new(every interval: NSTimeInterval, _ block: NSTimer -> Void) -> NSTimer {
var timer: NSTimer!
@nonobjc public class func newWithTimer(every interval: TimeInterval, _ block: @escaping (Timer) -> Void) -> Timer {
var timer: Timer!
timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + interval, interval, 0, 0) { _ in
block(timer)
}
return timer
}

// MARK: Manual scheduling
// MARK: Manual scheduling

/// Schedule this timer on the run loop
///
/// By default, the timer is scheduled on the current run loop for the default mode.
/// Specify `runLoop` or `modes` to override these defaults.

public func start(runLoop runLoop: NSRunLoop = NSRunLoop.currentRunLoop(), modes: String...) {
let modes = modes.isEmpty ? [NSDefaultRunLoopMode] : modes
public func start(_ runLoop: RunLoop = RunLoop.current, modes: RunLoopMode...) {
let modes = modes.isEmpty ? [RunLoopMode.defaultRunLoopMode] : modes

for mode in modes {
runLoop.addTimer(self, forMode: mode)
runLoop.add(self, forMode: mode)
}
}
}

// MARK: - Time extensions

extension Double {
public var millisecond: NSTimeInterval { return self / 1000 }
public var milliseconds: NSTimeInterval { return self / 1000 }
public var ms: NSTimeInterval { return self / 1000 }
public var millisecond: TimeInterval { return self / 1000 }
public var milliseconds: TimeInterval { return self / 1000 }
public var ms: TimeInterval { return self / 1000 }

public var second: NSTimeInterval { return self }
public var seconds: NSTimeInterval { return self }
public var second: TimeInterval { return self }
public var seconds: TimeInterval { return self }

public var minute: NSTimeInterval { return self * 60 }
public var minutes: NSTimeInterval { return self * 60 }
public var minute: TimeInterval { return self * 60 }
public var minutes: TimeInterval { return self * 60 }

public var hour: NSTimeInterval { return self * 3600 }
public var hours: NSTimeInterval { return self * 3600 }
public var hour: TimeInterval { return self * 3600 }
public var hours: TimeInterval { return self * 3600 }

public var day: NSTimeInterval { return self * 3600 * 24 }
public var days: NSTimeInterval { return self * 3600 * 24 }
public var day: TimeInterval { return self * 3600 * 24 }
public var days: TimeInterval { return self * 3600 * 24 }
}
6 changes: 3 additions & 3 deletions SwiftyTimer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Pod::Spec.new do |s|
s.version = '1.4.0'
s.license = 'MIT'
s.summary = 'Swifty API for NSTimer'
s.homepage = 'https://github.com/radex/SwiftyTimer'
s.homepage = 'https://github.com/OskarGroth/SwiftyTimer'
s.authors = { 'Radek Pietruszewski' => 'this.is@radex.io' }
s.source = { git: 'https://github.com/radex/SwiftyTimer.git', tag: s.version }
s.source = { git: 'https://github.com/OskarGroth/SwiftyTimer.git', tag: s.version }

s.requires_arc = true
s.ios.deployment_target = '8.0'
Expand All @@ -14,4 +14,4 @@ Pod::Spec.new do |s|
s.watchos.deployment_target = '2.0'

s.source_files = 'Sources/*.swift'
end
end
29 changes: 26 additions & 3 deletions SwiftyTimer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@
3E721AB21BF7255C008AF027 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0710;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Radosław Pietruszewski";
TargetAttributes = {
3E721ABA1BF7255D008AF027 = {
CreatedOnToolsVersion = 7.1;
LastSwiftMigration = 0800;
};
6E7E40891C84B1A20030CEBB = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
};
6E7E40991C84B3790030CEBB = {
CreatedOnToolsVersion = 7.2;
Expand Down Expand Up @@ -322,8 +324,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -352,6 +356,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -371,8 +376,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -393,6 +400,8 @@
MACOSX_DEPLOYMENT_TARGET = 10.9;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -405,6 +414,7 @@
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -417,6 +427,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -425,6 +436,7 @@
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -436,14 +448,15 @@
PRODUCT_BUNDLE_IDENTIFIER = io.radex.SwiftyTimer;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
6E7E40901C84B1A20030CEBB /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
Expand All @@ -458,14 +471,15 @@
PRODUCT_NAME = SwiftyTimer;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
6E7E40911C84B1A20030CEBB /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
Expand All @@ -480,13 +494,15 @@
PRODUCT_NAME = SwiftyTimer;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
6E7E40A01C84B3790030CEBB /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -498,6 +514,7 @@
PRODUCT_NAME = SwiftyTimer;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
};
Expand All @@ -507,6 +524,7 @@
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -518,6 +536,7 @@
PRODUCT_NAME = SwiftyTimer;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
};
Expand All @@ -527,6 +546,7 @@
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -538,6 +558,7 @@
PRODUCT_NAME = SwiftyTimer;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 4;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
Expand All @@ -547,6 +568,7 @@
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
"CODE_SIGN_IDENTITY[sdk=watchos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -558,6 +580,7 @@
PRODUCT_NAME = SwiftyTimer;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 4;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -11,8 +11,7 @@
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES"
hideIssues = "NO">
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6E7E40891C84B1A20030CEBB"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -11,8 +11,7 @@
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES"
hideIssues = "NO">
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6E7E40991C84B3790030CEBB"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -11,8 +11,7 @@
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES"
hideIssues = "NO">
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "6E7E40A71C84B4240030CEBB"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down