标签: method-swizzling

Swift - 仅当类符合使用方法调配的协议时才扩展类

我有一个名为 的协议NakedNavigationBar

\n\n

我还有一个扩展,可以扩展所有UIViewController符合NakedNavigationBar.

\n\n

问题是,在扩展中我想添加默认行为,以便在初始化时UIViewController,我们在UIViewController.

\n\n

这是我的协议和扩展:

\n\n
import UIKit\n\n\nprotocol NakedNavigationBar\n{\n\n}\n\nextension NakedNavigationBar where Self: UIViewController\n{\n    public override class func initialize()\n    {\n        struct Static\n        {\n            static var token: dispatch_once_t = 0\n        }\n\n        dispatch_once(&Static.token)\n        {\n            let originalSelector = #selector(viewWillAppear(_:))\n            let swizzledSelector = #selector(nakedNavigationBar_viewWillAppear(_:))\n\n            let originalMethod = class_getInstanceMethod(self, originalSelector)\n            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)\n\n            let didAddMethod = class_addMethod(self,\n                                               originalSelector,\n                                               method_getImplementation(swizzledMethod),\n                                               method_getTypeEncoding(swizzledMethod))\n\n            if didAddMethod\n            {\n                class_replaceMethod(self,\n                                    swizzledSelector,\n                                    method_getImplementation(originalMethod),\n                                    method_getTypeEncoding(originalMethod))\n            }\n            else\n            {\n                method_exchangeImplementations(originalMethod, …
Run Code Online (Sandbox Code Playgroud)

uiviewcontroller ios swift swift-extensions method-swizzling

3
推荐指数
1
解决办法
1108
查看次数

如何在 Objective C 中动态创建类别?

我正在开发一个 Objective-C 库项目(“MyLib”)。假设这个库包含在“TestApp”中。现在,“TestApp”还包含另一个名为“Xlib”的库。该 Xlib 有一个类 C1,其中有一个方法 m1。

//C1.h is part of Xlib
@interface C1
- (void) m1;
@end
Run Code Online (Sandbox Code Playgroud)

现在,每次调用 m1 时,MyLib 都应该执行一段代码。我的方法是,如果我在 MyLib 中创建一个类别:

@interface C1 (mycat)
@end

@implementation C1 (mycat)
+ (void) load{
    //code to swizzle method in class C1 from: @selector(m1) to: @selector(mycat_m1)
}
- (void) mycat_m1{
    /*
      insert code that I want to execute
    */
     [self mycat_m1]; //calling the original m1 implementation
}
@end
Run Code Online (Sandbox Code Playgroud)

问题: MyLib 没有类 C1。所以我无法构建 MyLib,因为我试图在不存在的类上创建一个类别。因此,编译错误。

所以,然后我尝试将上面的代码包装在里面:

#if defined(__has_include)
#if __has_include("C1.h")
    /* …
Run Code Online (Sandbox Code Playgroud)

objective-c swizzling ios objc-category method-swizzling

3
推荐指数
1
解决办法
465
查看次数

方法调整协议

我希望能够像textFieldDidBeginEditing:和那样使用Swizzle协议方法tableView:didSelectRowAtIndexPath:.如何才能做到这一点?

objective-c uikit ios method-swizzling

2
推荐指数
1
解决办法
1551
查看次数

不会调用NSMutableDictionary的Swizzled方法

我正在尝试调配NSMutableDictionary.我在这做错了什么?我正在尝试覆盖setObject:forKey:for NSMutableDictionary.

#import "NSMutableDictionary+NilHandled.h"
#import <objc/runtime.h>


@implementation NSMutableDictionary (NilHandled)

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

        SEL originalSelector = @selector(setObject:forKey:);
        SEL swizzledSelector = @selector(swizzledSetObject:forKey:);

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

        class_replaceMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));

        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);
        }
    });
}

- (void) swizzledSetObject:(id)anObject forKey:(id<NSCopying>)aKey
{
    [self swizzledSetObject:anObject?anObject:@"Nil" forKey:aKey];
}
Run Code Online (Sandbox Code Playgroud)

@结束

objective-c nsmutabledictionary swizzling ios method-swizzling

0
推荐指数
1
解决办法
168
查看次数