-
Notifications
You must be signed in to change notification settings - Fork 1
/
Swizzler.swift
35 lines (31 loc) · 1.33 KB
/
Swizzler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//
// Swizzler.swift
// DetailsViewer
//
// Created by Saagar Jha on 3/29/20.
// Copyright © 2020 Saagar Jha. All rights reserved.
//
import Foundation
/// Encapsulates a swizzled Objective-C method. Implementation is an IMP type
/// (i.e. an `@convention(c)` function pointer with the reciever as the first
/// parameter and selector as the second) while Block is an Objective-C block
/// replacement suitable to be passed to `imp_implementationWithBlock` (i.e. an
/// `@convention(block)` block with the reciever as the first parameter).
struct Swizzler<Implementation, Block> {
/// The original selector of the swizzled method.
let originalSelector: Selector
/// The original implementation of the swizzled method.
let originalImplementation: Implementation
/// Perform a swizzle.
/// - Parameters:
/// - class: The class to perform swizzling on
/// - selector: The selector to swizzle
/// - block: The code to replace the method with
init(class: AnyClass, selector: Selector, block: Block) {
originalSelector = selector
let method = class_getInstanceMethod(`class`, selector)!
let imp = imp_implementationWithBlock(block)
class_addMethod(`class`, Selector("swizzle_\(selector)"), imp, method_getTypeEncoding(method))
originalImplementation = unsafeBitCast(method_setImplementation(method, imp), to: Implementation.self)
}
}