-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSwizzler.swift
48 lines (40 loc) · 1.08 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
36
37
38
39
40
41
42
43
44
45
46
47
48
import Foundation
/**
A helper struct for method swizzling.
*/
struct Swizzler {
/**
Swizzles method by name.
- parameter method: Method
- parameter cls: Class
- parameter prefix: Unique prefix
- parameter prefix: Swizzling type, instance or class
*/
static func swizzle(cls: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
let originalMethod = class_getInstanceMethod(cls, originalSelector)
let swizzledMethod = class_getInstanceMethod(cls, swizzledSelector)
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
/**
DispatchQueue extension which implements dispatch_once functionality
*/
extension DispatchQueue {
private static var tokens = [String]()
/**
Executes a closure only once.
- parameter token: A unique token
- parameter closure: Closure to execute once
*/
class func once(token: String, closure: () -> Void) {
objc_sync_enter(self)
defer {
objc_sync_exit(self)
}
guard !tokens.contains(token) else {
return
}
tokens.append(token)
closure()
}
}