- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#import <objc/runtime.h>
@implementation Foo
+ (void)test
{
[Foo performSelector:@selector(foo)];
[Foo performSelector:@selector(foo) withObject:@"works"];
[Foo performSelector:@selector(foo1)];
[Foo performSelector:@selector(foo2)];
}
+ (BOOL)resolveClassMethod:(SEL)sel
{
NSLog(@"resolveClassMethod: %@", NSStringFromSelector(sel));
if (sel == @selector(foo)) {
class_addMethod(objc_getMetaClass("Foo"), sel, (IMP)fooIMP_withoutArgument, "s@#@@@");
return YES;
} else if (sel == @selector(foo1)) {
class_addMethod(objc_getMetaClass("Foo"), sel, (IMP)fooIMP_with1Argument, "Works");
return YES;
} else if (sel == @selector(foo2)) {
class_addMethod(objc_getMetaClass("Foo"), sel, (IMP)fooIMP_with2Arguments, "Not Works");
return YES;
}
return [super resolveClassMethod:sel];
}
void fooIMP_withoutArgument(id self, SEL _cmd)
{
NSLog(@"fooIMP_withoutArgument");
}
void fooIMP_with1Argument(id self, SEL _cmd, id arg)
{
NSLog(@"fooIMP_with1Argument");
}
void fooIMP_with2Arguments(id self, SEL _cmd, id arg1, id arg2)
{
NSLog(@"fooIMP_with2Argument");
}
@end
2018-02-27 16:41:31.638693+0800 Test[74840:2194481] resolveInstanceMethod: foo
2018-02-27 16:41:31.639201+0800 Test[74840:2194481] fooIMP_withoutArgument
2018-02-27 16:41:31.639228+0800 Test[74840:2194481] fooIMP_withoutArgument
2018-02-27 16:41:31.639490+0800 Test[74840:2194481] resolveInstanceMethod: foo1
2018-02-27 16:41:31.639519+0800 Test[74840:2194481] fooIMP_with1Argument
2018-02-27 16:41:31.639548+0800 Test[74840:2194481] resolveInstanceMethod: foo2
(lldb)
class_addMethod
函数不检查参数类型? IMP
有 1 个参数有效,但 2 个参数无效? 最佳答案
class_addMethod function does not check argument types?
id
在运行时——对象的类型检查发生在编译时,并且该信息不会保存在二进制文件中。
class_addMethod
是一个低级运行时函数,因此它不包括任何类型检查。没关系,因为这些运行时函数很少使用,并且仅在您确切知道自己在做什么以及因此期望什么类型的情况下才使用。
SEL is just a key point to IMP without other role?
SEL
是一个选择器,在 Objective-C 的典型实现中是一个字符串,在引擎盖下。
objc_msgSend
将此选择器解析为
IMP
,这只是一个函数,其主体包含方法的实现。
Why IMP with 1 argument work, but 2 arguments not?
foo2
的签名,也不描述到底出了什么问题,很难说。但是由于您似乎没有尝试调用
foo2
有了任何论据,结果最终会成为您想要的东西并不奇怪。
YES
添加方法后,但调用
super
的
resolveInstanceMethod:
的实现.如果该方法恰好返回
NO
,运行时会认为添加该方法不起作用。您应该返回
YES
而是在成功添加方法后。 (如果你不这样做,你应该调用
super
的
resolveClassMethod:
而不是
resolveInstanceMethod:
的实现。)
关于ios - ObjC class_addMethod 不检查参数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49003493/
#import @implementation Foo + (void)test { [Foo performSelector:@selector(foo)]; [Foo perfo
上手开发 iOS 一段时间后,我发现并不能只着眼于完成需求,利用闲暇之余多研究其他的开发技巧,才能在有限时间内提升自己水平。当然,“其他开发技巧”这个命题对于任何一个开发领域都感觉不找边际,而对于我
我使用运行时特性向类中添加了一个类方法,但是NSInvocation 无法使用该方法。我的代码是这样的: id metaClass = object_getClass((id)protocolClas
使用class_addMethod代码: class_addMethod(newClass, @selector(inputAccessoryView), accessoryImp, "@@:");
我正在尝试使用此处接受的答案:Dynamic UIMenuItems with @selector and dynamic methods但它给出了一个警告(并且它不起作用):函数'class_add
我正在尝试编写一些动态代码,用户可以在其中尝试从类的特定实例调用方法并在运行时解析它。检索信息的实现存在,但访问它的方法不存在,因为它是基于每个实例的。 例如,用户可能想调用类中不存在的名为“getS
当我使用 objc 运行时函数 class_addMethod() 将实现注入(inject)到 NSObject 的实例选择器时,它实际上将实现注入(inject)到实例选择器 AND 类选择器:
我是一名优秀的程序员,十分优秀!