我有一个名为 的协议NakedNavigationBar
。
我还有一个扩展,可以扩展所有UIViewController
符合NakedNavigationBar
.
问题是,在扩展中我想添加默认行为,以便在初始化时UIViewController
,我们在UIViewController
.
这是我的协议和扩展:
\n\nimport 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
我正在开发一个 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) 我希望能够像textFieldDidBeginEditing:
和那样使用Swizzle协议方法tableView:didSelectRowAtIndexPath:
.如何才能做到这一点?
我正在尝试调配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