简体   繁体   English

Cocoa pod 中的方法调配不工作

[英]Method swizzling not woking in Cocoa pod

I'm developing a pod which should use method swizzling at some point and I have a problem to call swizzled method when calling original one.我正在开发一个 pod,它应该在某些时候使用方法 swizzling,但在调用原始方法时我遇到了调用 swizzled 方法的问题。 I checked my swizzling in the simple POC (to be sure my swizzling is ok) and it works perfectly fine, but as cocoa pod (with mark use_framework in pod file even) it doesn't work, so I was debugging it and method exchange happens, it finds original method and swizzled method implementations, etc. but swizzled method doesn't get called which should happen in the main app, that has my pod.我在简单的 POC 中检查了我的 swizzling(以确保我的 swizzling 没问题),它工作得非常好,但是作为 cocoa pod(甚至在 pod 文件中带有标记 use_framework)它不起作用,所以我正在调试它和方法交换发生时,它会找到原始方法和 swizzled 方法实现等,但不会调用 swizzled 方法,这应该发生在我的 pod 主应用程序中。 method swizzling is written using objective-c Anyone had such problems?使用 objective-c 编写方法 swizzling有人遇到过这样的问题吗? Code from my POC我的 POC 中的代码

Swizzler调酒师

- (BOOL)swizzleClass:(Class)class
replaceInstanceMethod:(SEL)methodSelector1
          withMethod:(SEL)methodSelector2 {
    
    if (!class || !methodSelector1 || !methodSelector2) {
        NSLog(@"Nil Parameter(s) found when swizzling.");
        return NO;
    }
    
    Method method1 = class_getInstanceMethod(class, methodSelector1);
    Method method2 = class_getInstanceMethod(class, methodSelector2);
    
    if (method1 && method2) {
        method_exchangeImplementations(method1, method2);
        return YES;
        
    } else {
        NSLog(@"Swizzling Method(s) not found while swizzling class %@.", NSStringFromClass(class));
        return NO;
    }
}

Class category Class 类别

+ (void)load {
    static dispatch_once_t once_token;
    dispatch_once(&once_token,  ^{
        
        swizzler = [[Swizzler alloc] init];
         
         swizzleSuccess = [swizzler swizzleClass:self replaceInstanceMethod:@selector(wristLocation) withMethod: @selector(swizzled_wristLocation)];
         
         if (swizzleSuccess) {
             NSLog(@"Successss 🦭 swizzle");
         } else {
             NSLog(@"Cannot swizzle");
         }
        
    });
}

- (void)setLocation:(WKInterfaceDeviceWristLocation)location {
    testWristLocation =  [[NSNumber alloc] initWithInt: location];
}

- (WKInterfaceDeviceWristLocation)swizzled_wristLocation {
    NSLog(@"Called swizzled method 🐳.");
    [self swizzled_wristLocation];
    return [testWristLocation intValue];
}

Class where I call it Class 我称之为

@IBOutlet weak var locationLabel: WKInterfaceLabel!
    
    override func awake(withContext context: Any?) {
        
        WKInterfaceDevice.current().setLocation(WKInterfaceDeviceWristLocation.right)
        
        updateLocation()
    }
    
    @IBAction func checkLocation() {
        updateLocation()
    }
    
    dynamic private func updateLocation() {
        let location = WKInterfaceDevice.current().wristLocation
        switch location {
        case .left:
            self.locationLabel.setText("left")
        case .right:
            self.locationLabel.setText("right")
        @unknown default:
            self.locationLabel.setText("unknown")
        }
    }

So, I find out what was wrong here.所以,我发现这里出了什么问题。 The problem was in wrong target membership, I was swizzling in one target and expecting swizzled method to be called in another (which is basically another app).问题在于错误的目标成员资格,我在一个目标中调配,并期望在另一个目标中调用调配方法(这基本上是另一个应用程序)。 Right code sharing fixed the problem.正确的代码共享解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM