Skip to content

Commit f2d3943

Browse files
committed
Added docs for object hook methods
1 parent 11b8a93 commit f2d3943

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

Sources/InterposeKit/Interpose.swift

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,50 @@ public enum Interpose {
129129
// MARK: Object Hooks
130130
// ============================================================================ //
131131

132+
/// Prepares a hook for the specified method on an object.
133+
///
134+
/// Builds a block-based hook for an instance method available to the Objective-C runtime,
135+
/// with access to the original implementation.
136+
///
137+
/// The hook is returned in a pending state and must be applied using `apply()` to take effect.
138+
/// It can then be reverted using `revert()`.
139+
///
140+
/// InterposeKit installs the hook by creating a dynamic subclass at runtime and assigning it
141+
/// to the object. This ensures the hook only affects this specific object and leaves all other
142+
/// instances unchanged.
143+
///
144+
/// - Parameters:
145+
/// - object: The object on which to hook the method.
146+
/// - selector: The selector of the method to hook.
147+
/// - methodSignature: The expected C function type of the original method implementation.
148+
/// - hookSignature: The type of the hook block.
149+
/// - build: A closure that receives a proxy to the hook and returns the hook block.
150+
///
151+
/// - Returns: The prepared hook instance in the pending state.
152+
///
153+
/// - Throws: An ``InterposeError`` if the hook could not be prepared.
154+
///
155+
/// ### Example
156+
///
157+
/// ```swift
158+
/// let object = MyClass()
159+
/// let hook = try Interpose.prepareHook(
160+
/// on: object,
161+
/// for: #selector(MyClass.getValue),
162+
/// methodSignature: (@convention(c) (MyClass, Selector) -> Int).self,
163+
/// hookSignature: (@convention(block) (MyClass) -> Int).self
164+
/// ) { hook in
165+
/// return { `self` in
166+
/// print("Before")
167+
/// let value = hook.original(self, hook.selector)
168+
/// print("After")
169+
/// return value + 1
170+
/// }
171+
/// }
172+
///
173+
/// try hook.apply()
174+
/// try hook.revert()
175+
/// ```
132176
public static func prepareHook<MethodSignature, HookSignature>(
133177
on object: NSObject,
134178
for selector: Selector,
@@ -143,6 +187,48 @@ public enum Interpose {
143187
)
144188
}
145189

190+
/// Applies a hook for the specified method on an object.
191+
///
192+
/// Builds a block-based hook for an instance method available to the Objective-C runtime,
193+
/// with access to the original implementation.
194+
///
195+
/// The hook takes effect immediately and can later be reverted using `revert()`.
196+
///
197+
/// InterposeKit installs the hook by creating a dynamic subclass at runtime and assigning it
198+
/// to the object. This ensures the hook only affects this specific object and leaves all other
199+
/// instances unchanged.
200+
///
201+
/// - Parameters:
202+
/// - object: The object on which to hook the method.
203+
/// - selector: The selector of the method to hook.
204+
/// - methodSignature: The expected C function type of the original method implementation.
205+
/// - hookSignature: The type of the hook block.
206+
/// - build: A closure that receives a proxy to the hook and returns the hook block.
207+
///
208+
/// - Returns: The applied hook instance in the active state.
209+
///
210+
/// - Throws: An ``InterposeError`` if the hook could not be applied.
211+
///
212+
/// ### Example
213+
///
214+
/// ```swift
215+
/// let object = MyClass()
216+
/// let hook = try Interpose.applyHook(
217+
/// on: object,
218+
/// for: #selector(MyClass.getValue),
219+
/// methodSignature: (@convention(c) (MyClass, Selector) -> Int).self,
220+
/// hookSignature: (@convention(block) (MyClass) -> Int).self
221+
/// ) { hook in
222+
/// return { `self` in
223+
/// print("Before")
224+
/// let value = hook.original(self, hook.selector)
225+
/// print("After")
226+
/// return value + 1
227+
/// }
228+
/// }
229+
///
230+
/// try hook.revert()
231+
/// ```
146232
@discardableResult
147233
public static func applyHook<MethodSignature, HookSignature>(
148234
on object: NSObject,

0 commit comments

Comments
 (0)