AR89 AR89 - 5 months ago 9
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: http://nshipster.com/method-swizzling/.
I've created a class and a category, here is the code

#import <Foundation/Foundation.h>

@interface CustomClass : NSObject

-(void) originalMethod;

@end


Implementation of the class

#import "CustomClass.h"

@implementation CustomClass

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

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

@end


Category Header:

#import "CustomClass.h"

@interface CustomClass (CustomCategory)

-(void) newMethod;

@end


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,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}

@end


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

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.