Skip to content

Commit 11b8a93

Browse files
committed
Added docs for class hook methods
1 parent 7b13e6c commit 11b8a93

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

Sources/InterposeKit/Interpose.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,46 @@ public enum Interpose {
1111
// MARK: Class Hooks
1212
// ============================================================================ //
1313

14+
/// Prepares a hook for the specified method on a class.
15+
///
16+
/// Builds a block-based hook for an instance or class method available to the Objective-C
17+
/// runtime, with access to the original implementation.
18+
///
19+
/// The hook is returned in a pending state and must be applied using `apply()` to take effect.
20+
/// It can later be reverted using `revert()`.
21+
///
22+
/// - Parameters:
23+
/// - class: The class on which the method is defined.
24+
/// - selector: The selector of the method to hook.
25+
/// - methodKind: Whether the method is an instance or class method. Defaults to `.instance`.
26+
/// - methodSignature: The expected C function type of the original method implementation.
27+
/// - hookSignature: The type of the hook block.
28+
/// - build: A closure that receives a proxy to the hook and returns the hook block.
29+
///
30+
/// - Returns: The prepared hook instance in the pending state.
31+
///
32+
/// - Throws: An ``InterposeError`` if the hook could not be prepared.
33+
///
34+
/// ### Example
35+
///
36+
/// ```swift
37+
/// let hook = try Interpose.prepareHook(
38+
/// on: MyClass.self,
39+
/// for: #selector(MyClass.getValue),
40+
/// methodSignature: (@convention(c) (MyClass, Selector) -> Int).self,
41+
/// hookSignature: (@convention(block) (MyClass) -> Int).self
42+
/// ) { hook in
43+
/// return { `self` in
44+
/// print("Before")
45+
/// let value = hook.original(self, hook.selector)
46+
/// print("After")
47+
/// return value + 1
48+
/// }
49+
/// }
50+
///
51+
/// try hook.apply()
52+
/// try hook.revert()
53+
/// ```
1454
public static func prepareHook<MethodSignature, HookSignature>(
1555
on `class`: AnyClass,
1656
for selector: Selector,
@@ -26,6 +66,44 @@ public enum Interpose {
2666
)
2767
}
2868

69+
/// Applies a hook for the specified method on a class.
70+
///
71+
/// Builds a block-based hook for an instance or class method available to the Objective-C
72+
/// runtime, with access to the original implementation.
73+
///
74+
/// The hook takes effect immediately and can later be reverted using `revert()`.
75+
///
76+
/// - Parameters:
77+
/// - class: The class on which the method is defined.
78+
/// - selector: The selector of the method to hook.
79+
/// - methodKind: Whether the method is an instance or class method. Defaults to `.instance`.
80+
/// - methodSignature: The expected C function type of the original method implementation.
81+
/// - hookSignature: The type of the hook block.
82+
/// - build: A closure that receives a proxy to the hook and returns the hook block.
83+
///
84+
/// - Returns: The applied hook instance in the active state.
85+
///
86+
/// - Throws: An ``InterposeError`` if the hook could not be applied.
87+
///
88+
/// ### Example
89+
///
90+
/// ```swift
91+
/// let hook = try Interpose.applyHook(
92+
/// on: MyClass.self,
93+
/// for: #selector(MyClass.getValue),
94+
/// methodSignature: (@convention(c) (MyClass, Selector) -> Int).self,
95+
/// hookSignature: (@convention(block) (MyClass) -> Int).self
96+
/// ) { hook in
97+
/// return { `self` in
98+
/// print("Before")
99+
/// let value = hook.original(self, hook.selector)
100+
/// print("After")
101+
/// return value + 1
102+
/// }
103+
/// }
104+
///
105+
/// try hook.revert()
106+
/// ```
29107
@discardableResult
30108
public static func applyHook<MethodSignature, HookSignature>(
31109
on `class`: AnyClass,

0 commit comments

Comments
 (0)