forked from mbazaliy/MBSwizzler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBSwizzler.swift
42 lines (32 loc) · 1.39 KB
/
MBSwizzler.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
//
// MBSwizzler.swift
// SwizzlingExample
//
// Created by Max Bazaliy on 6/5/14.
// Copyright (c) 2014 Home. All rights reserved.
//
import Foundation
extension NSObject {
class func swizzleMethodSelector(origSelector: CString!, withSelector: CString!, forClass:AnyClass!) -> Bool {
var originalMethod: Method?
var swizzledMethod: Method?
originalMethod = class_getInstanceMethod(forClass, Selector.convertFromStringLiteral(origSelector))
swizzledMethod = class_getInstanceMethod(forClass, Selector.convertFromStringLiteral(withSelector))
if (originalMethod && swizzledMethod) {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
return true
}
return false
}
class func swizzleStaticMethodSelector(origSelector: CString!, withSelector: CString!, forClass:AnyClass!) -> Bool {
var originalMethod: Method?
var swizzledMethod: Method?
originalMethod = class_getClassMethod(forClass, Selector.convertFromStringLiteral(origSelector))
swizzledMethod = class_getClassMethod(forClass, Selector.convertFromStringLiteral(withSelector))
if (originalMethod && swizzledMethod) {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
return true
}
return false
}
}