Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 29 lines (22 sloc) 1.068 kB
de71513 @joshaber Initial commit.
authored
1 //
2 // NSObject+JAExtensions.m
3 //
4 // Created by Josh Abernathy on 8/18/10.
5 // Copyright (c) 2010 Maybe Apps. All rights reserved.
6 //
7
8 #import "NSObject+JAExtensions.h"
9 #import <objc/runtime.h>
10
11
12 @implementation NSObject (JAExtensions)
13
14 + (void)swapMethod:(SEL)originalSelector with:(SEL)newSelector {
15 Method originalMethod = class_getInstanceMethod(self, originalSelector);
16 Method newMethod = class_getInstanceMethod(self, newSelector);
17 const char *originalTypeEncoding = method_getTypeEncoding(originalMethod);
18 const char *newTypeEncoding = method_getTypeEncoding(newMethod);
19 NSAssert2(!strcmp(originalTypeEncoding, newTypeEncoding), @"Method type encodings must be the same: %s vs. %s", originalTypeEncoding, newTypeEncoding);
20
21 if(class_addMethod(self, originalSelector, method_getImplementation(newMethod), newTypeEncoding)) {
22 class_replaceMethod(self, newSelector, method_getImplementation(originalMethod), originalTypeEncoding);
23 } else {
24 method_exchangeImplementations(originalMethod, newMethod);
25 }
26 }
27
28 @end
Something went wrong with that request. Please try again.