Last active
October 11, 2017 23:18
-
-
Save vladimirgoncharov/22dc024c4204d77baaa8c6c15652087e to your computer and use it in GitHub Desktop.
Stub current time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Works only `NSDate()`! `Date()` will not be stubbed. Uses a Private API and should not be in main project target or your application will be rejected !!! | |
extension NSDate { | |
func stubAsCurrenDate(block: () -> Void) { | |
let customDateBlock: @convention(block) (AnyObject) -> NSDate = { _ in self } | |
let implementation = imp_implementationWithBlock(unsafeBitCast(customDateBlock, to: AnyObject.self)) | |
let method = class_getInstanceMethod(NSClassFromString("__NSPlaceholderDate"), #selector(NSObject.init)) | |
let oldImplementation = method_getImplementation(method) | |
method_setImplementation(method, implementation) | |
block() | |
method_setImplementation(method, oldImplementation) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment