ios - Swizzling 不适用于类方法

标签 ios objective-c swizzle

Swizzling 不执行动态方法交换。这是我使用的代码。我听说这是一个无法在 xcode 7 中的 XCTest 中进行依赖注入(inject)的解决方案。你能给我解释一下 DI(依赖)上的 Swizzling 示例吗?

#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];;
}

这里当我调用 [TNUserDetail isSignUpWithTwitter] 时,+(BOOL)isSignUpSwizzle 没有被调用,只有实际的方法被调用。怎么了。注意这两个方法都是类方法。

最佳答案

方法实例存在于调度表类中,但是 调度表 meta_class 中存在类方法,因此 您需要使用“元类”而不是 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

并调用[TNUserDetail printHello];打印'Hello World'

但是你的调配影响了整个项目。对于这种情况,我建议使用部分模拟 (OCMock)

关于ios - Swizzling 不适用于类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34648815/

相关文章:

objective-c - 释放错误 [NSPersistentStoreCoordinator initWithManagedObjectModel]

ios - 调试核心数据 __NSCFSet addObject nil 异常

ios - UILabel iOS 上的文字换行

ios - NEHotspotConfigurationManager 无法加入

ios - com.apple.developer.icloud-container-identifiers 和 com.apple.developer.ubiquity-container-identifiers 之间的区别

ios - Xcode 11 中没有这样的模块 'PackageDescription'

IOS UIPicker 列表超出 TableView 单元格?

iPhone 3D 指南针

ios - 如何在 Swift 中调配 init

ios - 如何从框架中调配 AppDelegate 的 didReceiveRemoteNotification