Skip to content

Instantly share code, notes, and snippets.

View MathVasc's full-sized avatar

Matheus de Vasconcelos MathVasc

View GitHub Profile
button.sendActions(for: .touchUpInside)
@MathVasc
MathVasc / TestForSendAction.swift
Last active May 23, 2020 19:19
testForSendAction
func testTargetSendAction() {
button.sendActions(for: .touchUpInside)
/*
Here you can validate if the button action
did execute with the right behavior
*/
XCTAssert(someCodeExecution)
}
@MathVasc
MathVasc / SendFakeAction.swift
Last active May 23, 2020 19:55
sendFakeActions
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
}
@MathVasc
MathVasc / SendFakeActionWithParameter.swift
Created May 23, 2020 19:55
sendFakeActionWithParameter
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
}
@MathVasc
MathVasc / TestSendFakeAction.swift
Created May 23, 2020 19:57
testSendFakeAction
func testTargetSendAction() {
button.sendFakeAction(for: .touchUpInside)
XCTAssert(someCodeExecution)
}
// An more OBJ-C approach
let aClass: AnyClass? = object_getClass(SomeClass())
// An more Swift approach
let aClass = SomeClass.self
@MathVasc
MathVasc / Selectors.swift
Last active June 8, 2020 18:01
Selectors
/*
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.
*/
let method: Method? = class_getInstanceMethod(aClass, selector)
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)
if let originalMethod = method1, let swizzledMethod = method2 {
method_exchangeImplementations(originalMethod, swizzledMethod)
}