MuraliMohan
MuraliMohan

Reputation: 1093

Swizzling is not working for class methods

Swizzling is not performing dynamic method exchanging . This the code i used.i heard that it is an solution where Dependency injection unable to do in XCTest in xcode 7. can you give me explanation on Swizzling over DI(Dependency) with example ?

#import "TNUserDetail+Swizzle.h"
#import <objc/runtime.h>

@implementation TNUserDetail (Swizzle)

+ (void) swizzleInstanceSelector:(SEL)originalSelector
                 withNewSelector:(SEL)newSelector
{
    Method originalMethod = class_getClassMethod(self, originalSelector);
    Method newMethod = class_getClassMethod(self, newSelector);

    BOOL methodAdded = class_addMethod([self class],
                                       originalSelector,
                                       method_getImplementation(newMethod),
                                       method_getTypeEncoding(newMethod));

    if (methodAdded) {
        class_replaceMethod([self class],
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

+(BOOL)isSignUpSwizzle {

    return sighUp;
}


Test
_____

@implementation TNSettingsViewControllerTests

- (void)setUp {
    [super setUp];

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    self.settingVC = [sb instantiateViewControllerWithIdentifier:@"TNSettingsViewController"];


    [self.settingVC performSelectorOnMainThread:@selector(loadView) withObject:nil waitUntilDone:YES];
    [self.settingVC performSelectorOnMainThread:@selector(viewWillAppear:) withObject:nil waitUntilDone:YES];
}

-(void)testTwitterConnectSwitchValueChanged
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        [TNUserDetail swizzleInstanceSelector:@selector(isSignUpWithTwitter) withNewSelector:@selector(isSignUpSwizzle)];
        [TNUserDetail isSignUpWithTwitter];
    });

    sighUp = YES;
    self.settingVC.twitterConnectSwitch.on = YES;
    [self.settingVC.twitterConnectSwitch sendActionsForControlEvents:UIControlEventValueChanged];;
}

Here when i call [TNUserDetail isSignUpWithTwitter] ,+(BOOL)isSignUpSwizzle is not being called and only actual method is being called. Whats wrong .Note both methods are class methods.

Upvotes: 0

Views: 1387

Answers (1)

ajjnix
ajjnix

Reputation: 462

Methods instance exist in dispatch table class, but class methods exist in dispatch table meta_class so you need use 'meta class' instead self(class).

#import "TNUserDetail.h"
#import <objc/runtime.h>

@implementation TNUserDetail

+ (void)swizzleInstanceSelector:(SEL)originalSelector withNewSelector:(SEL)newSelector {
    const char *className = [NSStringFromClass(self) UTF8String];
    Class clazz = objc_getMetaClass(className);
    Method originalMethod = class_getClassMethod(clazz, originalSelector);
    Method newMethod = class_getClassMethod(clazz, newSelector);

    BOOL methodAdded = class_addMethod(clazz,
                                       originalSelector,
                                       method_getImplementation(newMethod),
                                       method_getTypeEncoding(newMethod));

    if (methodAdded) {
        class_replaceMethod(clazz,
                            newSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

+ (void)load {
    [super load];
    [self swizzleInstanceSelector:@selector(printHello) withNewSelector:@selector(printHelloWorld)];
}

+ (void)printHello {
    NSLog(@"Hello");
}

+ (void)printHelloWorld {
    NSLog(@"Hello World");
}

@end

and call [TNUserDetail printHello]; print 'Hello World'

But your swizzling affects the entire project. For this case I recommendation use partial mocks (OCMock)

Upvotes: 6

Related Questions