AR89 AR89 - 1 year ago 46
Objective-C Question

Objective-C Method Swizzling example not working

I wanted to try the Method Swizzling to understand clearly how it works.
Looking at this code:
I've created a class and a category, here is the code

#import <Foundation/Foundation.h>

@interface CustomClass : NSObject

-(void) originalMethod;


Implementation of the class

#import "CustomClass.h"

@implementation CustomClass

-(id) init {
self = [super init];
return self;

-(void) originalMethod {
NSLog(@"I'm the original method");


Category Header:

#import "CustomClass.h"

@interface CustomClass (CustomCategory)

-(void) newMethod;


Category implementation

#import "CustomClass+CustomCategory.h"
#include <objc/runtime.h>

@implementation CustomClass (CustomCategory)

-(void) newMethod {
[self newMethod];
NSLog(@"I'm the new method");

+(void) load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];

// When swizzling a class method, use the following:
// Class class = object_getClass((id)self);

SEL originalSelector = @selector(originalMethod:);
SEL swizzledSelector = @selector(newMethod:);

Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

BOOL didAddMethod = class_addMethod(class,

if (didAddMethod) {
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);


and here is the main:

#import <Foundation/Foundation.h>
#import "CustomClass.h"
#import "CustomClass+CustomCategory.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {
CustomClass *cc = [[CustomClass alloc] init];
[cc newMethod];
return 0;

when I call
[cc newMethod]
, I get and infinite loop, which shouldn't happen according to the article I linked.
I can't see the error in this code.

Answer Source

The problem was in the syntax, instead of

SEL originalSelector = @selector(originalMethod:);
SEL swizzledSelector = @selector(newMethod:);

I should have written:

SEL originalSelector = @selector(originalMethod);
SEL swizzledSelector = @selector(newMethod);

xCode gave me just a warning, so I didn't think the selectors names were wrong.