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
| button.sendActions(for: .touchUpInside) |
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
| func testTargetSendAction() { | |
| button.sendActions(for: .touchUpInside) | |
| /* | |
| Here you can validate if the button action | |
| did execute with the right behavior | |
| */ | |
| XCTAssert(someCodeExecution) | |
| } |
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
| import UIKit | |
| public extension UIControl { | |
| func sendFakeAction(for controlEvent: UIControl.Event) { | |
| allTargets.forEach { target in | |
| guard let actions = actions(forTarget: target, forControlEvent: controlEvent), | |
| let responder = target.base as? NSObject else { | |
| return | |
| } |
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
| import UIKit | |
| public extension UIControl { | |
| func sendFakeAction(for controlEvent: UIControl.Event) { | |
| allTargets.forEach { target in | |
| guard let actions = actions(forTarget: target, forControlEvent: controlEvent), | |
| let responder = target.base as? NSObject else { | |
| return | |
| } |
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
| func testTargetSendAction() { | |
| button.sendFakeAction(for: .touchUpInside) | |
| XCTAssert(someCodeExecution) | |
| } |
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
| // An more OBJ-C approach | |
| let aClass: AnyClass? = object_getClass(SomeClass()) | |
| // An more Swift approach | |
| let aClass = SomeClass.self |
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
| /* | |
| An unsafe - and not recommended - way | |
| to get the selector is using a String. | |
| */ | |
| let selector: Selector = Selector(("SomeClass.someMethodName")) | |
| /* | |
| A safe - and recommended - way | |
| to get the selector is using the compiled method name. | |
| */ |
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
| let method: Method? = class_getInstanceMethod(aClass, selector) |
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
| let aClass: AnyClass? = object_getClass(SomeClass()) | |
| let selector1: Selector = #selector(SomeClass.someMethod) | |
| let selector2: Selector = #selector(SomeClass.someOtherMethod) | |
| let method1: Method? = class_getInstanceMethod(aClass, selector1) | |
| let method2: Method? = class_getInstanceMethod(aClass, selector2) |
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
| if let originalMethod = method1, let swizzledMethod = method2 { | |
| method_exchangeImplementations(originalMethod, swizzledMethod) | |
| } |