OBJC_EXPORT char * _Nonnull
method_copyReturnType(Method _Nonnull m)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
/**
* Returns a string describing a single parameter type of a method.
*
* @param m The method to inspect.
* @param index The index of the parameter to inspect.
*
* @return A C string describing the type of the parameter at index \e index, or \c NULL
* if method has no parameter index \e index. You must free the string with \c free().
*/
OBJC_EXPORT char * _Nullable
method_copyArgumentType(Method _Nonnull m, unsigned int index)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
/**
* Returns by reference a string describing a method’s return type.
*
* @param m The method you want to inquire about.
* @param dst The reference string to store the description.
* @param dst_len The maximum number of characters that can be stored in \e dst.
*
* @note The method’s return type string is copied to \e dst.
* \e dst is filled as if \c strncpy(dst, parameter_type, dst_len) were called.
*/
OBJC_EXPORT void
method_getReturnType(Method _Nonnull m, char * _Nonnull dst, size_t dst_len)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
/**
* Returns by reference a string describing a single parameter type of a method.
*
* @param m The method you want to inquire about.
* @param index The index of the parameter you want to inquire about.
* @param dst The reference string to store the description.
* @param dst_len The maximum number of characters that can be stored in \e dst.
*
* @note The parameter type string is copied to \e dst. \e dst is filled as if \c strncpy(dst, parameter_type, dst_len)
* were called. If the method contains no parameter with that index, \e dst is filled as
* if \c strncpy(dst, “”, dst_len) were called.
*/
OBJC_EXPORT void
method_getArgumentType(Method _Nonnull m, unsigned int index,
char * _Nullable dst, size_t dst_len)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
OBJC_EXPORT struct objc_method_description *_Nonnull
method_getDescription(Method _Nonnull m)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
/**
* Sets the implementation of a method.
*
* @param m The method for which to set an implementation.
* @param imp The implemention to set to this method.
*
* @return The previous implementation of the method.
*/
OBJC_EXPORT IMP_Nonnull
method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
/**
* Exchanges the implementations of two methods.
*
* @param m1 Method to exchange with second method.
* @param m2 Method to exchange with first method.
*
* @note This is an atomic version of the following:
* \code
* IMP imp1 = method_getImplementation(m1);
* IMP imp2 = method_getImplementation(m2);
* method_setImplementation(m1, imp2);
* method_setImplementation(m2, imp1);
* \endcode
*/
OBJC_EXPORT void
method_exchangeImplementations(Method _Null_unspecified /* _Nonnull */ m1, Method _Null_unspecified /* _Nonnull */ m2)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
Runtime 关于Method IMP 的应用示例
Demo1:
获取方法名字和获取方法的IMP 。 从objc_method结构体可知,method里有SEL 方法名 和IMP 实现方法的函数指针组成。所以我们可以从方法里拿到他的属性,然后玩一玩,下面就是针对这两个方法的小应用。
通过下面示例我们可以明白:Method SEL IMP 外在联系
method_getName(Method _Nonnull m)
method_getImplementation(Method _Nonnull m)
- (void)viewDidLoad {
[super viewDidLoad];
//首先我们早类里面找到该方法
Method ori_Method = class_getInstanceMethod([self class], @selector(myMethod:));
#if 0
//获取方法名 SEL
SEL oriMethodName = method_getName(ori_Method);
//根据方法名获取函数指针
IMP myMethodImp = [self methodForSelector:oriMethodName];
#else
//直接根据方法名获取函数指针 等同于IF 0
IMP myMethodImp = method_getImplementation(ori_Method);
#endif
//在该类中添加方法。
#if 0
class_addMethod([self class], @selector(testMethod:), myMethodImp, method_getTypeEncoding(ori_Method));
#else
class_addMethod([self class], @selector(testMethod:), myMethodImp, “V@😡”);
#endif
//在执行方法。
[self performSelector:@selector(testMethod:) withObject:@“7777”];
}
-(void)myMethod:(NSString *)myValue{
NSLog(@“myMethod:%@”,myValue);
}
打印结果:
2017-07-20 10:52:26.581146+0800 zyTest[8351:416437] myMethod:7777
Demo2:
中间关于获得方法参数,参数个数,参数返回类型就不赘述了,示例中有的会用到。主要介绍下面几个方法:
上面是GET IMP,那肯定可以设置方法的IMP
method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)
方法交换
method_exchangeImplementations(Method _Null_unspecified /* _Nonnull */ m1, Method _Null_unspecified /* _Nonnull */ m2)
//方法交换
-(void)demo2{
Method method1 = class_getInstanceMethod([self class], @selector(exchangeMethod1:));
Method method2 = class_getInstanceMethod([self class], @selector(exchangeMethod2:));
Method method3 = class_getInstanceMethod([self class], @selector(exchangeMethod3:));
Method method4 = class_getInstanceMethod([self class], @selector(exchangeMethod4:));
//方法替换 替换SEL 的IMP实现
class_replaceMethod([self class], @selector(exchangeMethod1:), method_getImplementation(method3), method_getTypeEncoding(method3));
//和class_replaceMethod 类似,替换method 的结构题IMP指针
method_setImplementation(method4, method_getImplementation(method2));
//方法交换
method_exchangeImplementations(method1, method2);
//猜一猜打印的什么
[self performSelector:method_getName(method1) withObject:@“Runtime Method Demo1” afterDelay:0.0];
[self exchangeMethod2:@“Runtime Method Demo2”];
[self exchangeMethod3:@“Runtime Method Demo3”];
[self exchangeMethod4:@“Runtime Method Demo4”];
}
-(void)exchangeMethod1:(id)str{
NSLog(@“exchangeMethod1:%@”,str);
}
-(void)exchangeMethod2:(id)num{
NSLog(@“exchangeMethod2:%@”,num);
}
-(void)exchangeMethod3:(id)f{
NSLog(@“exchangeMethod3:%@”,f);
}
-(void)exchangeMethod4:(id)w{
NSLog(@“exchangeMethod4:%@”,w);
}
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
更多面试题
**《350页前端校招面试题精编解析大全》**内容大纲主要包括 HTML,CSS,前端基础,前端核心,前端进阶,移动端开发,计算机基础,算法与数据结构,项目,职业发展等等
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-kwNndhp7-1710784257926)]
[外链图片转存中…(img-Sb5o2aZE-1710784257927)]
[外链图片转存中…(img-OC6O0uQa-1710784257927)]
[外链图片转存中…(img-UvmjGnAC-1710784257928)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-vm1bWmkD-1710784257928)]
更多面试题
**《350页前端校招面试题精编解析大全》**内容大纲主要包括 HTML,CSS,前端基础,前端核心,前端进阶,移动端开发,计算机基础,算法与数据结构,项目,职业发展等等
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
[外链图片转存中…(img-sBJwXtKR-1710784257929)]