【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
Objective-C中的runtime详解
本文介绍自己用到过的或者遇到过的runtime函数, 持续更新中...
首先要想使用runtime中的函数, 必须引入#import <objc/runtime.h>头文件.
1.objc打头的方法
1.1 objc_getClass()
OBJC_EXPORT Class objc_getClass(const char *name);
这个方法作用是根据类名字的字符串, 创建class进而可以创建对象. 如下代码所示:
const char *className = [@"ViewController" UTF8String];
Class class = objc_getClass(className);
id instance = [[class alloc] init];
NSLog(@"%@", [instance class]);
1.2 关联
/////////////////////////关联策略, 枚举类型的
typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
OBJC_ASSOCIATION_ASSIGN = 0, /**< Specifies a weak reference to the associated object. */
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.
* The association is not made atomically. */
OBJC_ASSOCIATION_COPY_NONATOMIC = 3, /**< Specifies that the associated object is copied.
* The association is not made atomically. */
OBJC_ASSOCIATION_RETAIN = 01401, /**< Specifies a strong reference to the associated object.
* The association is made atomically. */
OBJC_ASSOCIATION_COPY = 01403 /**< Specifies that the associated object is copied.
* The association is made atomically. */
};
/**
* Sets an associated value for a given object using a given key and association policy.
*
* @param object The source object for the association.
* @param key The key for the association.
* @param value The value to associate with the key key for object. Pass nil to clear an existing association.
* @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
*
* @see objc_setAssociatedObject
* @see objc_removeAssociatedObjects
*/
////////////////////////////////////////////////////////////////////
<!-- 设置关联对象 -->
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
/**
* Returns the value associated with a given object for a given key.
*
* @param object The source object for the association.
* @param key The key for the association.
*
* @return The value associated with the key \e key for \e object.
*
* @see objc_setAssociatedObject
*/
//////////////////////////////////////////////////////////////////// <!-- 根据对象, key获取关联对象 -->
OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
/**
* Removes all associations for a given object.
*
* @param object An object that maintains associated objects.
*
* @note The main purpose of this function is to make it easy to return an object
* to a "pristine state”. You should not use this function for general removal of
* associations from objects, since it also removes associations that other clients
* may have added to the object. Typically you should use \c objc_setAssociatedObject
* with a nil value to clear an association.
*
* @see objc_setAssociatedObject
* @see objc_getAssociatedObject
*/
////////////////////////////////////////////////////////////////////
<!-- 删除对象下, 所有的关联 -->
OBJC_EXPORT void objc_removeAssociatedObjects(id object)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
2.class打头的方法
2.1 class_copyPropertyList()
OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount);
这个方法可以根据一个Class和int指针类型参数, 获取参数列表和参数的个数, 返回值是objc_property_t类型的数组, 这种类型是objc_property结构体指针.
2.2 class_getName
OBJC_EXPORT const char *class_getName(Class cls);
返回一个类的类名字, 目前的理解, 和object_getClassName作用是相同的.
2.3 class_respondsToSelector()
OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel);
判断一个类是不是能相应sel方法, 返回值是BOOL类型的.
3.object打头的方法
3.1 object_getClassName
OBJC_EXPORT const char *object_getClassName(id obj);
返回给定对象所属类的类名字符串(char *类型).
4.property打头的方法
4.1 property_getName()
OBJC_EXPORT const char *property_getName(objc_property_t property);
参数是objc_property_t, 返回值是参数对应的char *类型的字符串. 得到参数对应的字符串之后, 可以利用kvc进行赋值和取值.
5.method打头的方法
5.1 method_exchangeImplementations()
这个方法的作用是, 交换两个方法的实现, 交换这个过程是原子操作. 返回值是void, 参数是要交换的两个方法, 方法类型是Method.
/**
* 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 m1, Method m2) ;
5.2 class_getInstanceMethod()
根据Class和SEL, 获取一个实例对象的Method, 返回值是Method.
//如果本对象没有这个SEL方法, class_getInstanceMethod这个方法会去查找superclass是否包含这个方法, 但是class_copyMethodList, 不会去父类中查找.
OBJC_EXPORT Method class_getInstanceMethod(Class cls, SEL name);
5.3 class_getClassMethod()
class_getClassMethod这个方法用来获取类方法, 返回值是Method; class_getInstanceMethod这个方法用来获取实例方法, 返回值是Method.
OBJC_EXPORT Method class_getClassMethod(Class cls, SEL name)
来源:oschina
链接:https://my.oschina.net/u/2501614/blog/649545