iOS Runtime Method IMP指针详解

打印 上一主题 下一主题

主题 1310|帖子 1310|积分 3930

* Returns a string describing a method’s parameter and return types.
*
* @param m The method to inspect.
*
* @return A C string. The string may be \c NULL.
*/
OBJC_EXPORT const char * _Nullable
method_getTypeEncoding(Method _Nonnull m)
OBJC_AVAILABLE(10.5,2.0,9.0,1.0,2.0);
/**
* Returns the number of arguments accepted by a method.
*
* @param m A pointer to a \c Method data structure. Pass the method in question.
*
* @return An integer containing the number of arguments accepted by the given method.
*/
OBJC_EXPORT unsignedint
method_getNumberOfArguments(Method _Nonnull m)
OBJC_AVAILABLE(10.0,2.0,9.0,1.0,2.0);
/**
* Returns a string describing a method’s return type.
*
* @param m The method to inspect.
*
* @return A C string describing the return type. You must free the string with \c free().
*/
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 methodForSelectorriMethodName];
#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@

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

星球的眼睛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表