Interpose

final public class Interpose

Interpose is a modern library to swizzle elegantly in Swift.

Methods are hooked via replacing the implementation, instead of the usual exchange. Supports both swizzling classes and individual objects.

  • Stores swizzle hooks and executes them at once.

    Declaration

    Swift

    public let `class`: AnyClass
  • Lists all hooks for the current interpose class object.

    Declaration

    Swift

    public private(set) var hooks: [AnyHook] { get }
  • If Interposing is object-based, this is set.

    Declaration

    Swift

    public let object: AnyObject?
  • Initializes an instance of Interpose for a specific class. If builder is present, apply() is automatically called.

    Declaration

    Swift

    public init(_ class: AnyClass, builder: ((Interpose) throws -> Void)? = nil) throws
  • Initialize with a single object to interpose.

    Declaration

    Swift

    public init(_ object: NSObject, builder: ((Interpose) throws -> Void)? = nil) throws
  • Hook an @objc dynamic instance method via selector name on the current class.

    Declaration

    Swift

    @discardableResult
    public func hook<MethodSignature, HookSignature>(
        _ selName: String,
        methodSignature: MethodSignature.Type = MethodSignature.self,
        hookSignature: HookSignature.Type = HookSignature.self,
        _ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
        throws -> TypedHook<MethodSignature, HookSignature>
  • Hook an @objc dynamic instance method via selector on the current class.

    Declaration

    Swift

    @discardableResult
    public func hook<MethodSignature, HookSignature> (
        _ selector: Selector,
        methodSignature: MethodSignature.Type = MethodSignature.self,
        hookSignature: HookSignature.Type = HookSignature.self,
        _ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
        throws -> TypedHook<MethodSignature, HookSignature>
  • Prepares a hook, but does not call apply immediately.

    Declaration

    Swift

    @discardableResult
    public func prepareHook<MethodSignature, HookSignature> (
        _ selector: Selector,
        methodSignature: MethodSignature.Type = MethodSignature.self,
        hookSignature: HookSignature.Type = HookSignature.self,
        _ implementation: (TypedHook<MethodSignature, HookSignature>) -> HookSignature?)
        throws -> TypedHook<MethodSignature, HookSignature>
  • Apply all stored hooks.

    Declaration

    Swift

    @discardableResult
    public func apply(_ hook: ((Interpose) throws -> Void)? = nil) throws -> Interpose
  • Revert all stored hooks.

    Declaration

    Swift

    @discardableResult
    public func revert(_ hook: ((Interpose) throws -> Void)? = nil) throws -> Interpose
  • A hook to an instance method and stores both the original and new implementation.

    See more

    Declaration

    Swift

    final public class ClassHook<MethodSignature, HookSignature> : TypedHook<MethodSignature, HookSignature>
    extension Interpose.ClassHook: CustomDebugStringConvertible

Logging

  • Logging uses print and is minimal.

    Declaration

    Swift

    public static var isLoggingEnabled: Bool
  • A hook to an instance method of a single object, stores both the original and new implementation. Think about: Multiple hooks for one object

    See more

    Declaration

    Swift

    final public class ObjectHook<MethodSignature, HookSignature> : TypedHook<MethodSignature, HookSignature>
    extension Interpose.ObjectHook: CustomDebugStringConvertible

Interpose Class Load Watcher

  • Interpose a class once available. Class is passed via classParts string array.

    Declaration

    Swift

    @discardableResult
    public class func whenAvailable(_ classParts: [String],
                                    builder: @escaping (Interpose) throws -> Void) throws -> Waiter
  • Interpose a class once available. Class is passed via classParts string array, with completion handler.

    Declaration

    Swift

    @discardableResult
    public class func whenAvailable(_ classParts: [String],
                                    builder: @escaping (Interpose) throws -> Void,
                                    completion: (() -> Void)? = nil) throws -> Waiter
  • Interpose a class once available. Class is passed via className string.

    Declaration

    Swift

    @discardableResult
    public class func whenAvailable(_ className: String,
                                    builder: @escaping (Interpose) throws -> Void) throws -> Waiter
  • Interpose a class once available. Class is passed via className string, with completion handler.

    Declaration

    Swift

    @discardableResult
    public class func whenAvailable(_ className: String,
                                    builder: @escaping (Interpose) throws -> Void,
                                    completion: (() -> Void)? = nil) throws -> Waiter
  • Helper that stores hooks to a specific class and executes them once the class becomes available.

    Declaration

    Swift

    public struct Waiter