This repository was archived by the owner on Sep 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathNSObject+MethodSwizzling.m
More file actions
51 lines (49 loc) · 1.58 KB
/
NSObject+MethodSwizzling.m
File metadata and controls
51 lines (49 loc) · 1.58 KB
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
49
50
51
/**
* @file NSObject+MethodSwizzling.h
*
* @copyright 2018-2019 Bill Zissimopoulos
*/
/*
* This file is part of EnergyBar.
*
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License version 3 as published by the Free Software
* Foundation.
*/
#import "NSObject+MethodSwizzling.h"
#import <objc/runtime.h>
@implementation NSObject (MethodSwizzling)
+ (BOOL)swizzleInstanceMethod:(SEL)oldsel withMethod:(SEL)newsel
{
Method oldmeth = class_getInstanceMethod(self, oldsel);
if (NULL == oldmeth)
{
NSLog(@"%s error: selector %s not found in class %@",
__PRETTY_FUNCTION__, sel_getName(oldsel), self);
return NO;
}
Method newmeth = class_getInstanceMethod(self, newsel);
if (NULL == newmeth)
{
NSLog(@"%s error: selector %s not found in class %@",
__PRETTY_FUNCTION__, sel_getName(newsel), self);
return NO;
}
class_addMethod(self, oldsel,
method_getImplementation(oldmeth), method_getTypeEncoding(oldmeth));
class_addMethod(self, newsel,
method_getImplementation(newmeth), method_getTypeEncoding(newmeth));
/* IMPORTANT NOTE:
* Must perform class_getInstanceMethod() again in case methods
* were added from a superclass to this class.
*/
method_exchangeImplementations(
class_getInstanceMethod(self, oldsel),
class_getInstanceMethod(self, newsel));
return YES;
}
+ (BOOL)swizzleClassMethod:(SEL)oldsel withMethod:(SEL)newsel
{
return [object_getClass(self) swizzleInstanceMethod:oldsel withMethod:newsel];
}
@end