前言
學習開發只有兩件事是值得做下去的。第一是國外人的書,第二是官方的文件。
Runtime
SEL
定義
/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;
複製程式碼
sel_getName
/// Returns the name of the method specified by a given selector.
// 通過 SEL 返回方法名字
OBJC_EXPORT const char * _Nonnull sel_getName(SEL _Nonnull sel)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
NSLog(@"--- %s", sel_getName(@selector(testFormat)));
複製程式碼
列印:
TestProject[22016:6798169] --- testFormat
複製程式碼
sel_registerName
/**
* Registers a method with the Objective-C runtime system, maps the method
* name to a selector, and returns the selector value.
*
* @param str A pointer to a C string. Pass the name of the method you wish to register.
*
* @return A pointer of type SEL specifying the selector for the named method.
*
* @note You must register a method name with the Objective-C runtime system to obtain the
* method’s selector before you can add the method to a class definition. If the method name
* has already been registered, this function simply returns the selector.
*/
// 註冊一個方法,對這個runtime系統返回一個SEL,如果這個方法的名字早已經被註冊,那麼這個函式只會返回 SEL
OBJC_EXPORT SEL _Nonnull sel_registerName(const char * _Nonnull str)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
((void (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("testFormat"));
複製程式碼
列印:
TestProject[22427:6817642] testFormat
複製程式碼
object_getClassName
/**
* Returns the class name of a given object.
*
* @param obj An Objective-C object.
*
* @return The name of the class of which \e obj is an instance.
*/
// 返回這個例項化類的名字
OBJC_EXPORT const char * _Nonnull object_getClassName(id _Nullable obj)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
object_getIndexedIvars
/**
* Returns a pointer to any extra bytes allocated with an instance given object.
*
* @param obj An Objective-C object.
*
* @return A pointer to any extra bytes allocated with \e obj. If \e obj was
* not allocated with any extra bytes, then dereferencing the returned pointer is undefined.
*
* @note This function returns a pointer to any extra bytes allocated with the instance
* (as specified by \c class_createInstance with extraBytes>0). This memory follows the
* object's ordinary ivars, but may not be adjacent to the last ivar.
* @note The returned pointer is guaranteed to be pointer-size aligned, even if the area following
* the object's last ivar is less aligned than that. Alignment greater than pointer-size is never
* guaranteed, even if the area following the object's last ivar is more aligned than that.
* @note In a garbage-collected environment, the memory is scanned conservatively.
*/
// 得到一個
OBJC_EXPORT void * _Nullable object_getIndexedIvars(id _Nullable obj)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARC_UNAVAILABLE;
複製程式碼
sel_getUid
/**
* Registers a method name with the Objective-C runtime system.
*
* @param str A pointer to a C string. Pass the name of the method you wish to register.
*
* @return A pointer of type SEL specifying the selector for the named method.
*
* @note The implementation of this method is identical to the implementation of \c sel_registerName.
* @note Prior to OS X version 10.0, this method tried to find the selector mapped to the given name
* and returned \c NULL if the selector was not found. This was changed for safety, because it was
* observed that many of the callers of this function did not check the return value for \c NULL.
*/
// 向 runtime系統根據字串註冊一個方法,返回SEL
OBJC_EXPORT SEL _Nonnull sel_getUid(const char * _Nonnull str)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
((void (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_getUid("testFormat"));
複製程式碼
列印:
TestProject[22472:6820361] testFormat
複製程式碼
sel_isMapped
/**
* Identifies a selector as being valid or invalid.
*
* @param sel The selector you want to identify.
*
* @return YES if selector is valid and has a function implementation, NO otherwise.
*
* @warning On some platforms, an invalid reference (to invalid memory addresses) can cause
* a crash.
*/
// 判斷 SEL 是否存在在 SEL Dictionary
OBJC_EXPORT BOOL sel_isMapped(SEL _Nonnull sel)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
SEL sel;
if (sel_isMapped(sel)) {
NSLog(@"有效");
}
複製程式碼
列印:
沒有列印
複製程式碼
IMP
/// A pointer to the function of a method implementation.
#if !OBJC_OLD_DISPATCH_PROTOTYPES
typedef void (*IMP)(void /* id, SEL, ... */ );
#else
typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...);
#endif
複製程式碼
IMP 指標指向方法的具體的實現
Method
定義
/// An opaque type that represents a method in a class definition.
typedef struct objc_method *Method;
複製程式碼
方法
結構
struct objc_method {
// 方法名字
SEL _Nonnull method_name OBJC2_UNAVAILABLE;
// 方法型別
char * _Nullable method_types OBJC2_UNAVAILABLE;
// 方法指標
IMP _Nonnull method_imp OBJC2_UNAVAILABLE;
}
複製程式碼
class_getInstanceMethod
/**
* Returns a specified instance method for a given class.
*
* @param cls The class you want to inspect.
* @param name The selector of the method you want to retrieve.
*
* @return The method that corresponds to the implementation of the selector specified by
* \e name for the class specified by \e cls, or \c NULL if the specified class or its
* superclasses do not contain an instance method with the specified selector.
*
* @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
*/
// 獲取類的例項化方法
OBJC_EXPORT Method _Nullable
class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Method methodI = class_getInstanceMethod([self class], @selector(viewWillAppear:));
複製程式碼
列印:
(lldb) po methodI
0x000000010ca31528
複製程式碼
class_getClassMethod
/**
* Returns a pointer to the data structure describing a given class method for a given class.
*
* @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve.
* @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve.
*
* @return A pointer to the \c Method data structure that corresponds to the implementation of the
* selector specified by aSelector for the class specified by aClass, or NULL if the specified
* class or its superclasses do not contain an instance method with the specified selector.
*
* @note Note that this function searches superclasses for implementations,
* whereas \c class_copyMethodList does not.
*/
// 獲取類方法
OBJC_EXPORT Method _Nullable
class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Method method = class_getClassMethod([Person class], @selector(test));
複製程式碼
列印:
(lldb) po method
0x0000000106c633d8
複製程式碼
class_getMethodImplementation
/**
* Returns the function pointer that would be called if a
* particular message were sent to an instance of a class.
*
* @param cls The class you want to inspect.
* @param name A selector.
*
* @return The function pointer that would be called if \c [object name] were called
* with an instance of the class, or \c NULL if \e cls is \c Nil.
*
* @note \c class_getMethodImplementation may be faster than \c method_getImplementation(class_getInstanceMethod(cls, name)).
* @note The function pointer returned may be a function internal to the runtime instead of
* an actual method implementation. For example, if instances of the class do not respond to
* the selector, the function pointer returned will be part of the runtime's message forwarding machinery.
*/
// 得到指向方法實現的指標
OBJC_EXPORT IMP _Nullable
class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
IMP imp = class_getMethodImplementation([self class], @selector(viewWillAppear:));
複製程式碼
列印:
(lldb) po imp
(UIKit`-[UIViewController viewWillAppear:])
複製程式碼
class_getMethodImplementation_stret
/**
* Returns the function pointer that would be called if a particular
* message were sent to an instance of a class.
*
* @param cls The class you want to inspect.
* @param name A selector.
*
* @return The function pointer that would be called if \c [object name] were called
* with an instance of the class, or \c NULL if \e cls is \c Nil.
*/
// 返回方法的具體的實現
OBJC_EXPORT IMP _Nullable
class_getMethodImplementation_stret(Class _Nullable cls, SEL _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
OBJC_ARM64_UNAVAILABLE;
複製程式碼
程式碼:
複製程式碼
列印:
複製程式碼
class_respondsToSelector
/**
* Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
*
* @param cls The class you want to inspect.
* @param sel A selector.
*
* @return \c YES if instances of the class respond to the selector, otherwise \c NO.
*
* @note You should usually use \c NSObject's \c respondsToSelector: or \c instancesRespondToSelector:
* methods instead of this function.
*/
// 返回一個 Boolean 判斷該類是否實現了指定的方法
OBJC_EXPORT BOOL
class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
BOOL isYES = class_respondsToSelector([self class], @selector(viewWillAppear:));
複製程式碼
列印:
(lldb) po isYES
YES
複製程式碼
class_copyMethodList
/**
* Describes the instance methods implemented by a class.
*
* @param cls The class you want to inspect.
* @param outCount On return, contains the length of the returned array.
* If outCount is NULL, the length is not returned.
*
* @return An array of pointers of type Method describing the instance methods
* implemented by the class—any instance methods implemented by superclasses are not included.
* The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
*
* If cls implements no instance methods, or cls is Nil, returns NULL and *outCount is 0.
*
* @note To get the class methods of a class, use \c class_copyMethodList(object_getClass(cls), &count).
* @note To get the implementations of methods that may be implemented by superclasses,
* use \c class_getInstanceMethod or \c class_getClassMethod.
*/
// 得到類的方法列表
OBJC_EXPORT Method _Nonnull * _Nullable
class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int count;
Method *methodList = class_copyMethodList([self class], &count);
for (unsigned int i = 0; i < count; i++) {
Method method = methodList[i];
NSLog(@"%s", sel_getName(method_getName(method)));
}
複製程式碼
列印:
TestProject[44285:8024690] testFormat
TestProject[44285:8024690] didReceiveMe
TestProject[44285:8024690] viewDidLoad
複製程式碼
method_setImplementation
/**
* 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);
複製程式碼
程式碼:
Method m1 = class_getInstanceMethod([TestClass class], @selector(testLog1));
Method m2 = class_getInstanceMethod([TestClass class], @selector(testLog2));
IMP imp1 = method_getImplementation(m1);
IMP imp2 = method_getImplementation(m2);
method_setImplementation(m1, imp2);
method_setImplementation(m2, imp1);
複製程式碼
method_exchangeImplementations
Method method1 = class_getInstanceMethod([NSString class], @selector(lowercaseString));
Method method2 = class_getInstanceMethod([NSString class], @selector(uppercaseString));
method_exchangeImplementations(method1, method2);//交換兩個方法後,兩個方法的功能相反
複製程式碼
Ivar
定義
/// An opaque type that represents an instance variable.
typedef struct objc_ivar *Ivar;
複製程式碼
結構
struct objc_ivar {
char * _Nullable ivar_name OBJC2_UNAVAILABLE;
char * _Nullable ivar_type OBJC2_UNAVAILABLE;
int ivar_offset OBJC2_UNAVAILABLE;
#ifdef __LP64__
int space OBJC2_UNAVAILABLE;
#endif
}
複製程式碼
方法
object_getIvar
/**
* Reads the value of an instance variable in an object.
*
* @param obj The object containing the instance variable whose value you want to read.
* @param ivar The Ivar describing the instance variable whose value you want to read.
*
* @return The value of the instance variable specified by \e ivar, or \c nil if \e object is \c nil.
*
* @note \c object_getIvar is faster than \c object_getInstanceVariable if the Ivar
* for the instance variable is already known.
*/
1. 讀取物件中,例項變數的值
OBJC_EXPORT id _Nullable
object_getIvar(id _Nullable obj, Ivar _Nonnull ivar)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
id mantest = object_getIvar(p1, ivar);
NSLog(@"%@", mantest);
複製程式碼
列印:
TestProject[22563:6825947] chen test
複製程式碼
object_getInstanceVariable
/**
* Obtains the value of an instance variable of a class instance.
*
* @param obj A pointer to an instance of a class. Pass the object containing
* the instance variable whose value you wish to obtain.
* @param name A C string. Pass the name of the instance variable whose value you wish to obtain.
* @param outValue On return, contains a pointer to the value of the instance variable.
*
* @return A pointer to the \c Ivar data structure that defines the type and name of
* the instance variable specified by \e name.
*/
// 獲取例項的例項變數的值
OBJC_EXPORT Ivar _Nullable
object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
void * _Nullable * _Nullable outValue)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARC_UNAVAILABLE;
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
複製程式碼
object_setInstanceVariable
/**
* Changes the value of an instance variable of a class instance.
*
* @param obj A pointer to an instance of a class. Pass the object containing
* the instance variable whose value you wish to modify.
* @param name A C string. Pass the name of the instance variable whose value you wish to modify.
* @param value The new value for the instance variable.
*
* @return A pointer to the \c Ivar data structure that defines the type and
* name of the instance variable specified by \e name.
*
* @note Instance variables with known memory management (such as ARC strong and weak)
* use that memory management. Instance variables with unknown memory management
* are assigned as if they were unsafe_unretained.
*/
// 給類動態的新增一個變數
OBJC_EXPORT Ivar _Nullable
object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name,
void * _Nullable value)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARC_UNAVAILABLE;
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
object_setInstanceVariableWithStrongDefault(p1, "_manTest", @"shuaibi");
複製程式碼
object_setIvar
/**
* Sets the value of an instance variable in an object.
*
* @param obj The object containing the instance variable whose value you want to set.
* @param ivar The Ivar describing the instance variable whose value you want to set.
* @param value The new value for the instance variable.
*
* @note Instance variables with known memory management (such as ARC strong and weak)
* use that memory management. Instance variables with unknown memory management
* are assigned as if they were unsafe_unretained.
* @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
* for the instance variable is already known.
*/
// 可以給私有變數設定值 速度超過 object_setInstanceVariable
OBJC_EXPORT void
object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
object_setIvar(p1, ivar, @"shuaibi");
複製程式碼
object_setIvarWithStrongDefault
/**
* Sets the value of an instance variable in an object.
*
* @param obj The object containing the instance variable whose value you want to set.
* @param ivar The Ivar describing the instance variable whose value you want to set.
* @param value The new value for the instance variable.
*
* @note Instance variables with known memory management (such as ARC strong and weak)
* use that memory management. Instance variables with unknown memory management
* are assigned as if they were strong.
* @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
* for the instance variable is already known.
*/
// 設定變數中例項變數的值 / iOS 10.0+
OBJC_EXPORT void
object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar,
id _Nullable value)
OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
if (@available(iOS 10.0, *)) {
object_setIvarWithStrongDefault(p1, ivar, @"chen123");
} else {
// Fallback on earlier versions
}
複製程式碼
object_setInstanceVariableWithStrongDefault
/**
* Changes the value of an instance variable of a class instance.
*
* @param obj A pointer to an instance of a class. Pass the object containing
* the instance variable whose value you wish to modify.
* @param name A C string. Pass the name of the instance variable whose value you wish to modify.
* @param value The new value for the instance variable.
*
* @return A pointer to the \c Ivar data structure that defines the type and
* name of the instance variable specified by \e name.
*
* @note Instance variables with known memory management (such as ARC strong and weak)
* use that memory management. Instance variables with unknown memory management
* are assigned as if they were strong.
*/
// 在MRC環境下使用,更改例項的例項變數的值
OBJC_EXPORT Ivar _Nullable
object_setInstanceVariableWithStrongDefault(id _Nullable obj,
const char * _Nonnull name,
void * _Nullable value)
OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0)
OBJC_ARC_UNAVAILABLE;
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
if (@available(iOS 10.0, *)) {
object_setInstanceVariableWithStrongDefault(p1, "_manTest", @"shuaibi");
} else {
// Fallback on earlier versions
}
複製程式碼
object_getInstanceVariable
/**
* Obtains the value of an instance variable of a class instance.
*
* @param obj A pointer to an instance of a class. Pass the object containing
* the instance variable whose value you wish to obtain.
* @param name A C string. Pass the name of the instance variable whose value you wish to obtain.
* @param outValue On return, contains a pointer to the value of the instance variable.
*
* @return A pointer to the \c Ivar data structure that defines the type and name of
* the instance variable specified by \e name.
*/
// 獲取例項變數的值 MRC 環境下有用
OBJC_EXPORT Ivar _Nullable
object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
void * _Nullable * _Nullable outValue)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARC_UNAVAILABLE;
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@" --- %@", object_getIvar(p1, ivar));
複製程式碼
列印:
TestProject[22787:6839305] --- chen test
複製程式碼
ivar_getName
/* Working with Instance Variables */
/**
* Returns the name of an instance variable.
*
* @param v The instance variable you want to enquire about.
*
* @return A C string containing the instance variable's name.
*/
// 返回實時變數的名稱
OBJC_EXPORT const char * _Nullable
ivar_getName(Ivar _Nonnull v)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@"%s", ivar_getName(ivar));
複製程式碼
列印:
TestProject[22909:6849954] _manTest
複製程式碼
ivar_getTypeEncoding
/**
* Returns the type string of an instance variable.
*
* @param v The instance variable you want to enquire about.
*
* @return A C string containing the instance variable's type encoding.
*
* @note For possible values, see Objective-C Runtime Programming Guide > Type Encodings.
*/
OBJC_EXPORT const char * _Nullable
ivar_getTypeEncoding(Ivar _Nonnull v)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@"%s", ivar_getTypeEncoding(ivar));
複製程式碼
列印:
TestProject[23004:6857088] @"NSString"
複製程式碼
ivar_getOffset
/**
* Returns the offset of an instance variable.
*
* @param v The instance variable you want to enquire about.
*
* @return The offset of \e v.
*
* @note For instance variables of type \c id or other object types, call \c object_getIvar
* and \c object_setIvar instead of using this offset to access the instance variable data directly.
*/
// 得到例項變數的偏移量
OBJC_EXPORT ptrdiff_t
ivar_getOffset(Ivar _Nonnull v)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@"%ld", ivar_getOffset(ivar));
複製程式碼
列印:
TestProject[23488:6907490] 8
複製程式碼
Category
定義
/// An opaque type that represents a category.
typedef struct objc_category *Category;
複製程式碼
結構
struct objc_category {
// 分類名字
char * _Nonnull category_name OBJC2_UNAVAILABLE;
// 類名
char * _Nonnull class_name OBJC2_UNAVAILABLE;
// objc 例項方法列表
struct objc_method_list * _Nullable instance_methods OBJC2_UNAVAILABLE;
// objc 類方法列表
struct objc_method_list * _Nullable class_methods OBJC2_UNAVAILABLE;
// 協議列表
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
複製程式碼
objc_property_t
定義
/// An opaque type that represents an Objective-C declared property.
typedef struct objc_property *objc_property_t;
複製程式碼
protocol_getProperty
/**
* Returns the specified property of a given protocol.
*
* @param proto A protocol.
* @param name The name of a property.
* @param isRequiredProperty \c YES searches for a required property, \c NO searches for an optional property.
* @param isInstanceProperty \c YES searches for an instance property, \c NO searches for a class property.
*
* @return The property specified by \e name, \e isRequiredProperty, and \e isInstanceProperty for \e proto,
* or \c NULL if none of \e proto's properties meets the specification.
*/
// 根據協議,屬性名字返回一個屬性
OBJC_EXPORT objc_property_t _Nullable
protocol_getProperty(Protocol * _Nonnull proto,
const char * _Nonnull name,
BOOL isRequiredProperty, BOOL isInstanceProperty)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocolA = objc_getProtocol("CatDelegate");
objc_property_t pro = protocol_getProperty(protocolA, "testMark", YES, YES);
NSLog(@"%s", property_getName(pro));
複製程式碼
列印:
TestProject[25760:731912] testMark
複製程式碼
protocol_copyPropertyList
/**
* Returns an array of the required instance properties declared by a protocol.
*
* @note Identical to
* \code
* protocol_copyPropertyList2(proto, outCount, YES, YES);
* \endcode
*/
// 獲取一個指定協議的所有的屬性,不包括方法
OBJC_EXPORT objc_property_t _Nonnull * _Nullable
protocol_copyPropertyList(Protocol * _Nonnull proto,
unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocolA = objc_getProtocol("CatDelegate");
unsigned int outCount;
objc_property_t *propertyList = protocol_copyPropertyList(protocolA, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
objc_property_t property = propertyList[i];
NSLog(@"name --- %s", property_getName(property));
}
複製程式碼
列印:
TestProject[26269:782402] name --- testMark
複製程式碼
protocol_copyPropertyList2
/**
* Returns an array of properties declared by a protocol.
*
* @param proto A protocol.
* @param outCount Upon return, contains the number of elements in the returned array.
* @param isRequiredProperty \c YES returns required properties, \c NO returns optional properties.
* @param isInstanceProperty \c YES returns instance properties, \c NO returns class properties.
*
* @return A C array of pointers of type \c objc_property_t describing the properties declared by \e proto.
* Any properties declared by other protocols adopted by this protocol are not included. The array contains
* \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
* If the protocol declares no matching properties, \c NULL is returned and \c *outCount is \c 0.
*/
// 獲取協議的所有的屬性,分為required 和 optional。
這個有點雞肋我試了 optional 不起作用
OBJC_EXPORT objc_property_t _Nonnull * _Nullable
protocol_copyPropertyList2(Protocol * _Nonnull proto,
unsigned int * _Nullable outCount,
BOOL isRequiredProperty, BOOL isInstanceProperty)
OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);
複製程式碼
程式碼:
Protocol *protocolA = objc_getProtocol("CatDelegate");
unsigned int outCount;
if (@available(iOS 10.0, *)) {
objc_property_t *propertyList = protocol_copyPropertyList2(protocolA, &outCount, NO, YES);
for (unsigned int i = 0; i < outCount; i++) {
objc_property_t property = propertyList[i];
NSLog(@"name --- %s", property_getName(property));
}
} else {
// Fallback on earlier versions
}
複製程式碼
列印:
TestProject[26895:821115] name --- optionalMark
TestProject[26895:821115] name --- testMark
複製程式碼
objc_class
結構
struct objc_class {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class _Nullable super_class OBJC2_UNAVAILABLE;
const char * _Nonnull name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE;
struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE;
struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE;
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
複製程式碼
object_copy
/**
* Returns a copy of a given object.
*
* @param obj An Objective-C object.
* @param size The size of the object \e obj.
*
* @return A copy of \e obj.
*/
// 物件的 copy 屬於深 copy
OBJC_EXPORT id _Nullable object_copy(id _Nullable obj, size_t size)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARC_UNAVAILABLE;
複製程式碼
程式碼:
NSLog(@"self --- %p", self);
NSLog(@"object_copy --- %p", object_copy(self, sizeof(self)));
複製程式碼
列印:
TestProject[23793:6934176] self --- 0x7f9f8c4049e0
TestProject[23793:6934176] object_copy --- 0x7f9f8c70a1d0
複製程式碼
object_dispose
/**
* Frees the memory occupied by a given object.
*
* @param obj An Objective-C object.
*
* @return nil
*/
// 釋放給定物件佔用的記憶體
OBJC_EXPORT id _Nullable
object_dispose(id _Nullable obj)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
OBJC_ARC_UNAVAILABLE;
複製程式碼
程式碼:
NSLog(@"%@",object_dispose(p1));
複製程式碼
列印:
(lldb) po p1
<Person: 0x60000000ec50>
2018-03-09 17:15:38.690 YZTestProject[24032:6953790] (null)
(lldb) po p1
0x000060000000ec50
複製程式碼
object_getClass
/**
* Returns the class of an object.
*
* @param obj The object you want to inspect.
*
* @return The class object of which \e object is an instance,
* or \c Nil if \e object is \c nil.
*/
// 返回物件的類
OBJC_EXPORT Class _Nullable
object_getClass(id _Nullable obj)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
NSLog(@"%@", object_getClass(p1));
複製程式碼
列印:
TestProject[24120:6960612] Person
複製程式碼
object_setClass
/**
* Sets the class of an object.
*
* @param obj The object to modify.
* @param cls A class object.
*
* @return The previous value of \e object's class, or \c Nil if \e object is \c nil.
*/
OBJC_EXPORT Class _Nullable
object_setClass(id _Nullable obj, Class _Nonnull cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
NSLog(@"\n p1 = %@", p1);
Class c1 = object_setClass(p1, [Cat class]);
NSLog(@"\n c1 = %@ \n p1 = %@", c1, p1);
複製程式碼
列印:
TestProject[24490:6987169]
p1 = <Person: 0x60000000a580>
TestProject[24490:6987169]
c1 = Person
p1 = <Cat: 0x60000000a580>
複製程式碼
object_isClass
/**
* Returns whether an object is a class object.
*
* @param obj An Objective-C object.
*
* @return true if the object is a class or metaclass, false otherwise.
*/
// 判斷是否是一個不為空的類物件
OBJC_EXPORT BOOL
object_isClass(id _Nullable obj)
OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
BOOL isp1 = object_isClass([p1 class]);
Person * p2;
BOOL isp2 = object_isClass([p2 class]);
NSString * p3 = @"test";
BOOL isp3 = object_isClass([p3 class]);
NSLog(@"%i", isp1);
NSLog(@"%i", isp2);
NSLog(@"%i", isp3);
複製程式碼
列印:
TestProject[24619:6998034] 1
TestProject[24619:6998034] 0
TestProject[24619:6998034] 1
複製程式碼
objc_getClass
/* Obtaining Class Definitions */
/**
* Returns the class definition of a specified class.
*
* @param name The name of the class to look up.
*
* @return The Class object for the named class, or \c nil
* if the class is not registered with the Objective-C runtime.
*
* @note \c objc_getClass is different from \c objc_lookUpClass in that if the class
* is not registered, \c objc_getClass calls the class handler callback and then checks
* a second time to see whether the class is registered. \c objc_lookUpClass does
* not call the class handler callback.
*
* @warning Earlier implementations of this function (prior to OS X v10.0)
* terminate the program if the class does not exist.
*/
OBJC_EXPORT Class _Nullable
objc_getClass(const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Cat *cat1 = [[Cat alloc] init];
Class c1 = object_getClass(cat1);
NSLog(@"%@", c1);
NSLog(@"%@", cat1.name);
複製程式碼
列印:
TestProject[24927:7020395] Cat
TestProject[24927:7020395] testH
複製程式碼
objc_getMetaClass
/**
* Returns the metaclass definition of a specified class.
*
* @param name The name of the class to look up.
*
* @return The \c Class object for the metaclass of the named class, or \c nil if the class
* is not registered with the Objective-C runtime.
*
* @note If the definition for the named class is not registered, this function calls the class handler
* callback and then checks a second time to see if the class is registered. However, every class
* definition must have a valid metaclass definition, and so the metaclass definition is always returned,
* whether it’s valid or not.
*/
// 得到一個類的元類
OBJC_EXPORT Class _Nullable
objc_getMetaClass(const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Cat *cat1 = [[Cat alloc] init];
Class c1 = objc_getMetaClass("Cat");
NSLog(@"%@", c1);
NSLog(@"%@", cat1.name);
複製程式碼
列印:
TestProject[25712:7077004] Cat
TestProject[25712:7077004] testH
複製程式碼
objc_lookUpClass
/**
* Returns the class definition of a specified class.
*
* @param name The name of the class to look up.
*
* @return The Class object for the named class, or \c nil if the class
* is not registered with the Objective-C runtime.
*
* @note \c objc_getClass is different from this function in that if the class is not
* registered, \c objc_getClass calls the class handler callback and then checks a second
* time to see whether the class is registered. This function does not call the class handler callback.
*/
// 獲取指定的類,如果沒有註冊則返回 nil
OBJC_EXPORT Class _Nullable
objc_lookUpClass(const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Class cl1 = objc_lookUpClass("Ca");
Class cl2 = objc_lookUpClass("Cat");
複製程式碼
objc_getRequiredClass
/**
* Returns the class definition of a specified class.
*
* @param name The name of the class to look up.
*
* @return The Class object for the named class.
*
* @note This function is the same as \c objc_getClass, but kills the process if the class is not found.
* @note This function is used by ZeroLink, where failing to find a class would be a compile-time link error without ZeroLink.
*/
// 獲取指定類,如果這個類不存在則崩潰
OBJC_EXPORT Class _Nonnull
objc_getRequiredClass(const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Class cl1 = objc_getRequiredClass("Ca");
複製程式碼
objc_getClassList
/**
* Obtains the list of registered class definitions.
*
* @param buffer An array of \c Class values. On output, each \c Class value points to
* one class definition, up to either \e bufferCount or the total number of registered classes,
* whichever is less. You can pass \c NULL to obtain the total number of registered class
* definitions without actually retrieving any class definitions.
* @param bufferCount An integer value. Pass the number of pointers for which you have allocated space
* in \e buffer. On return, this function fills in only this number of elements. If this number is less
* than the number of registered classes, this function returns an arbitrary subset of the registered classes.
*
* @return An integer value indicating the total number of registered classes.
*
* @note The Objective-C runtime library automatically registers all the classes defined in your source code.
* You can create class definitions at runtime and register them with the \c objc_addClass function.
*
* @warning You cannot assume that class objects you get from this function are classes that inherit from \c NSObject,
* so you cannot safely call any methods on such classes without detecting that the method is implemented first.
*/
// 獲取所有已經註冊的類
OBJC_EXPORT int
objc_getClassList(Class _Nonnull * _Nullable buffer, int bufferCount)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Class *bufferClass;
int numClasses;
numClasses = objc_getClassList(NULL, 0);
if(numClasses > 0) {
bufferClass = (Class *)malloc(sizeof(Class)*numClasses);
numClasses = objc_getClassList(bufferClass, numClasses);
NSLog(@"numer of classes: %d", numClasses);
for (int i = 0; i < numClasses; i++) {
Class cls = bufferClass[i];
NSLog(@"class name: %s", class_getName(cls));
}
free(bufferClass);
}
複製程式碼
輸出結果:
TestProject[41171:7767871] numer of classes: 4739
複製程式碼
objc_copyClassList
/**
* Creates and returns a list of pointers to all registered class definitions.
*
* @param outCount An integer pointer used to store the number of classes returned by
* this function in the list. It can be \c nil.
*
* @return A nil terminated array of classes. It must be freed with \c free().
*
* @see objc_getClassList
*/
// 獲取所有已經註冊的類
OBJC_EXPORT Class _Nonnull * _Nullable
objc_copyClassList(unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int outCount;
Class *classes = objc_copyClassList(&outCount);
for (int i = 0; i < outCount; i++) {
NSLog(@"%s", class_getName(classes[i]));
}
複製程式碼
列印:
TestProject[41322:7782252] JSExport
TestProject[41322:7782252] NSLeafProxy
TestProject[41322:7782252] NSProxy
TestProject[41322:7782252] _UITargetedProxy
TestProject[41322:7782252] _UIViewServiceReplyControlTrampoline
複製程式碼
class_getName
/* Working with Classes */
/**
* Returns the name of a class.
*
* @param cls A class object.
*
* @return The name of the class, or the empty string if \e cls is \c Nil.
*/
// 返回一個類的名字
OBJC_EXPORT const char * _Nonnull
class_getName(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
NSLog(@"%s", class_getName([self class]));
複製程式碼
列印:
TestProject[41376:7786142] ViewController
複製程式碼
class_isMetaClass
/**
* Returns a Boolean value that indicates whether a class object is a metaclass.
*
* @param cls A class object.
*
* @return \c YES if \e cls is a metaclass, \c NO if \e cls is a non-meta class,
* \c NO if \e cls is \c Nil.
*/
// 判斷輸出的類是否為元類
OBJC_EXPORT BOOL
class_isMetaClass(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
if (class_isMetaClass(objc_getMetaClass("Cat"))) {
NSLog(@"YES");
}
複製程式碼
列印:
TestProject[41512:7792603] YES
複製程式碼
class_getSuperclass
/**
* Returns the superclass of a class.
*
* @param cls A class object.
*
* @return The superclass of the class, or \c Nil if
* \e cls is a root class, or \c Nil if \e cls is \c Nil.
*
* @note You should usually use \c NSObject's \c superclass method instead of this function.
*/
// 得到這個類的父類
OBJC_EXPORT Class _Nullable
class_getSuperclass(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
NSLog(@"%@", class_getSuperclass([self class]));
複製程式碼
列印:
TestProject[41571:7797664] UIViewController
複製程式碼
class_setSuperclass
/**
* Sets the superclass of a given class.
*
* @param cls The class whose superclass you want to set.
* @param newSuper The new superclass for cls.
*
* @return The old superclass for cls.
*
* @warning You should not use this function.
*/
// 給類設定新的父類
OBJC_EXPORT Class _Nonnull
class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper)
__OSX_DEPRECATED(10.5, 10.5, "not recommended")
__IOS_DEPRECATED(2.0, 2.0, "not recommended")
__TVOS_DEPRECATED(9.0, 9.0, "not recommended")
__WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")
__BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended");
複製程式碼
程式碼:
NSLog(@"%@", class_getSuperclass(NSClassFromString(@"ViewController")));
class_setSuperclass(NSClassFromString(@"ViewController"), NSClassFromString(@"UIImageView"));
NSLog(@"%@", class_getSuperclass(NSClassFromString(@"ViewController")));
複製程式碼
列印:
TestProject[41901:7839232] UIViewController
TestProject[41901:7839232] UIImageView
複製程式碼
class_getVersion
/**
* Returns the version number of a class definition.
*
* @param cls A pointer to a \c Class data structure. Pass
* the class definition for which you wish to obtain the version.
*
* @return An integer indicating the version number of the class definition.
*
* @see class_setVersion
*/
// 獲取類的版本號 待續...
OBJC_EXPORT int
class_getVersion(Class _Nullable cls)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
NSLog(@"%d", class_getVersion([self class]));
複製程式碼
列印:
TestProject[42051:7849333] 0
複製程式碼
class_setVersion
/**
* Sets the version number of a class definition.
*
* @param cls A pointer to an Class data structure.
* Pass the class definition for which you wish to set the version.
* @param version An integer. Pass the new version number of the class definition.
*
* @note You can use the version number of the class definition to provide versioning of the
* interface that your class represents to other classes. This is especially useful for object
* serialization (that is, archiving of the object in a flattened form), where it is important to
* recognize changes to the layout of the instance variables in different class-definition versions.
* @note Classes derived from the Foundation framework \c NSObject class can set the class-definition
* version number using the \c setVersion: class method, which is implemented using the \c class_setVersion function.
*/
// 設定類的版本號
OBJC_EXPORT void
class_setVersion(Class _Nullable cls, int version)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
NSLog(@"%d", class_getVersion([self class]));
class_setVersion([self class], 2);
NSLog(@"%d", class_getVersion([self class]));
複製程式碼
版本:
TestProject[42247:7871127] 0
TestProject[42247:7871127] 2
複製程式碼
class_getInstanceSize
/**
* Returns the size of instances of a class.
*
* @param cls A class object.
*
* @return The size in bytes of instances of the class \e cls, or \c 0 if \e cls is \c Nil.
*/
// 返回例項類的大小,以位元組為單位
OBJC_EXPORT size_t
class_getInstanceSize(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
NSLog(@"%ld", class_getInstanceSize([self class]));
複製程式碼
列印:
TestProject[42305:7873713] 760
複製程式碼
class_getInstanceVariable
/**
* Returns the \c Ivar for a specified instance variable of a given class.
*
* @param cls The class whose instance variable you wish to obtain.
* @param name The name of the instance variable definition to obtain.
*
* @return A pointer to an \c Ivar data structure containing information about
* the instance variable specified by \e name.
*/
OBJC_EXPORT Ivar _Nullable
class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name)
OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
複製程式碼
列印:
(lldb) po ivar
0x00000001098a1460
複製程式碼
class_getClassVariable
/**
* Returns the Ivar for a specified class variable of a given class.
*
* @param cls The class definition whose class variable you wish to obtain.
* @param name The name of the class variable definition to obtain.
*
* @return A pointer to an \c Ivar data structure containing information about the class variable specified by \e name.
*/
// 獲取引數 cls 類的變數名為name的變數
OBJC_EXPORT Ivar _Nullable
class_getClassVariable(Class _Nullable cls, const char * _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Ivar viewIvar = class_getClassVariable([UIView class], "isa");
id getViewIvar = object_getIvar([UIView class], viewIvar);
NSLog(@"%d", class_isMetaClass(getViewIvar));
NSLog(@"%@", getViewIvar);
複製程式碼
列印:
TestProject[42676:7900606] 1
TestProject[42676:7900606] UIView
複製程式碼
class_copyIvarList
/**
* Describes the instance variables declared by a class.
*
* @param cls The class to inspect.
* @param outCount On return, contains the length of the returned array.
* If outCount is NULL, the length is not returned.
*
* @return An array of pointers of type Ivar describing the instance variables declared by the class.
* Any instance variables declared by superclasses are not included. The array contains *outCount
* pointers followed by a NULL terminator. You must free the array with free().
*
* If the class declares no instance variables, or cls is Nil, NULL is returned and *outCount is 0.
*/
// 一個 Ivar 型別的指標陣列,用於描述該類宣告的例項變數,必須用free釋放該陣列
OBJC_EXPORT Ivar _Nonnull * _Nullable
class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int count;
Ivar *ivars = class_copyIvarList([Person class], &count);
for (unsigned int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
const char *type = ivar_getTypeEncoding(ivar);
NSLog(@"型別為%s 的 %s", type, name);
}
free(ivars);
複製程式碼
列印:
TestProject[42937:7913951] 型別為@"NSString" 的 _manTest
複製程式碼
Protocol
定義:
typedef struct objc_object Protocol;
複製程式碼
class_conformsToProtocol
/**
* Returns a Boolean value that indicates whether a class conforms to a given protocol.
*
* @param cls The class you want to inspect.
* @param protocol A protocol.
*
* @return YES if cls conforms to protocol, otherwise NO.
*
* @note You should usually use NSObject's conformsToProtocol: method instead of this function.
*/
// 返回一個 Boolean 值,檢查某個類是否遵從某個協議
OBJC_EXPORT BOOL
class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocol = NSProtocolFromString(@"UITableViewDataSource");
BOOL isConforms = class_conformsToProtocol([UITableViewController class], protocol);
複製程式碼
列印:
(lldb) po isConforms
YES
複製程式碼
class_copyProtocolList
/**
* Describes the protocols adopted by a class.
*
* @param cls The class you want to inspect.
* @param outCount On return, contains the length of the returned array.
* If outCount is NULL, the length is not returned.
*
* @return An array of pointers of type Protocol* describing the protocols adopted
* by the class. Any protocols adopted by superclasses or other protocols are not included.
* The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
*
* If cls adopts no protocols, or cls is Nil, returns NULL and *outCount is 0.
*/
// 得到一個類遵守協議的個數
OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int count;
Protocol **protocolList = class_copyProtocolList([UITableViewController class], &count);
for (unsigned int i = 0; i < count; i++) {
Protocol *pro = protocolList[i];
NSLog(@"%s", protocol_getName(pro));
}
複製程式碼
列印:
TestProject[19640:180921] _UIKeyboardAutoRespondingScrollViewController
TestProject[19640:180921] UITableViewFocusDelegateLegacy
TestProject[19640:180921] UIViewControllerPreviewingDelegate
TestProject[19640:180921] UIViewControllerPreviewingDelegate_Deprecated
TestProject[19640:180921] UITableViewDelegate
TestProject[19640:180921] UITableViewDataSource
複製程式碼
objc_getProtocol
/* Working with Protocols */
/**
* Returns a specified protocol.
*
* @param name The name of a protocol.
*
* @return The protocol named \e name, or \c NULL if no protocol named \e name could be found.
*
* @note This function acquires the runtime lock.
*/
// 獲取名稱為 name 的協議
OBJC_EXPORT Protocol * _Nullable
objc_getProtocol(const char * _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocol = objc_getProtocol("UITableViewDataSource");
複製程式碼
列印:
(lldb) po protocol
<Protocol: 0x107dbf5e0>
複製程式碼
objc_copyProtocolList
/**
* Returns an array of all the protocols known to the runtime.
*
* @param outCount Upon return, contains the number of protocols in the returned array.
*
* @return A C array of all the protocols known to the runtime. The array contains \c *outCount
* pointers followed by a \c NULL terminator. You must free the list with \c free().
*
* @note This function acquires the runtime lock.
*/
// 返回執行時系統所有的協議
OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
objc_copyProtocolList(unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int outCount;
Protocol **protocolList = objc_copyProtocolList(&outCount);
for (unsigned int i = 0; i < outCount; i++) {
Protocol *protocol = protocolList[i];
NSLog(@"%s", protocol_getName(protocol));
}
複製程式碼
列印:
TestProject[23401:517412] UIVideoEditorControllerDelegate
TestProject[23401:517412] _UIIVCResponseDelegate
TestProject[23401:517412] _UITouchable
TestProject[23401:517412] WebOpenPanelResultListener
TestProject[23401:517412] SBSRemoteAlertClientHandle
TestProject[23401:517412] _UIContentContainerInternal
TestProject[23401:517412] _UIKeyboardAutoRespondingScrollView
TestProject[23401:517412] CAMLWriterDelegate
TestProject[23401:517412] OS_nw_endpoint_resolver
TestProject[23401:517412] _UIActivityHelperDelegate
複製程式碼
protocol_conformsToProtocol
/**
* Returns a Boolean value that indicates whether one protocol conforms to another protocol.
*
* @param proto A protocol.
* @param other A protocol.
*
* @return \c YES if \e proto conforms to \e other, otherwise \c NO.
*
* @note One protocol can incorporate other protocols using the same syntax
* that classes use to adopt a protocol:
* \code
* @protocol ProtocolName < protocol list >
* \endcode
* All the protocols listed between angle brackets are considered part of the ProtocolName protocol.
*/
// 判斷一個協議是否遵從了另外一個協議,第一個協議是否遵守了第二個協議
OBJC_EXPORT BOOL
protocol_conformsToProtocol(Protocol * _Nullable proto,
Protocol * _Nullable other)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *tableViewDelegate = objc_getProtocol("UITableViewDelegate");
Protocol *scrollViewDelegate = objc_getProtocol("UIScrollViewDelegate");
BOOL isConform = protocol_conformsToProtocol(tableViewDelegate, scrollViewDelegate);
複製程式碼
列印:
(lldb) po isConform
YES
點進去,可以看到第一個協議確實遵守了第二個協議
複製程式碼
protocol_isEqual
/**
* Returns a Boolean value that indicates whether two protocols are equal.
*
* @param proto A protocol.
* @param other A protocol.
*
* @return \c YES if \e proto is the same as \e other, otherwise \c NO.
*/
// 判斷兩個協議是否相等
OBJC_EXPORT BOOL
protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocolA = objc_getProtocol("UITableViewDataSource");
Protocol *protocolB = NSProtocolFromString(@"UITableViewDataSource");
BOOL isEqual = protocol_isEqual(protocolA, protocolB);
複製程式碼
列印:
(lldb) po isEqual
YES
複製程式碼
protocol_getName
/**
* Returns the name of a protocol.
*
* @param p A protocol.
*
* @return The name of the protocol \e p as a C string.
*/
根據協議得到協議的名稱
OBJC_EXPORT const char * _Nonnull
protocol_getName(Protocol * _Nonnull proto)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocolA = objc_getProtocol("UITableViewDataSource");
NSLog(@"%s", protocol_getName(protocolA));
複製程式碼
列印:
TestProject[23900:551699] UITableViewDataSource
複製程式碼
protocol_copyProtocolList
/**
* Returns an array of the protocols adopted by a protocol.
*
* @param proto A protocol.
* @param outCount Upon return, contains the number of elements in the returned array.
*
* @return A C array of protocols adopted by \e proto. The array contains \e *outCount pointers
* followed by a \c NULL terminator. You must free the array with \c free().
* If the protocol declares no properties, \c NULL is returned and \c *outCount is \c 0.
*/
// 返回協議所遵守的協議,以陣列的形式展現
OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
protocol_copyProtocolList(Protocol * _Nonnull proto,
unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocolA = objc_getProtocol("CatDelegate");
unsigned int outCount;
Protocol **protocolList = protocol_copyProtocolList(protocolA, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
Protocol *protocol = protocolList[i];
NSLog(@"name --- %s", protocol_getName(protocol));
}
複製程式碼
列印:
TestProject[26997:828030] name --- NSObject
複製程式碼
objc_allocateProtocol
/**
* Creates a new protocol instance that cannot be used until registered with
* \c objc_registerProtocol()
*
* @param name The name of the protocol to create.
*
* @return The Protocol instance on success, \c nil if a protocol
* with the same name already exists.
* @note There is no dispose method for this.
*/
// 建立一個新的協議例項,但是需要通過 objc_registerProtocol 註冊使用
OBJC_EXPORT Protocol * _Nullable
objc_allocateProtocol(const char * _Nonnull name)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocol = objc_allocateProtocol("PersonDelegate");
複製程式碼
列印:
(lldb) po protocol
<__IncompleteProtocol: 0x6080000a84c0>
複製程式碼
objc_registerProtocol
/**
* Registers a newly constructed protocol with the runtime. The protocol
* will be ready for use and is immutable after this.
*
* @param proto The protocol you want to register.
*/
// 註冊協議
OBJC_EXPORT void
objc_registerProtocol(Protocol * _Nonnull proto)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocol = objc_allocateProtocol("PersonDelegate");
// 宣告這個協議還是不可以用的,還是要註冊這個協議的
objc_registerProtocol(protocol);
複製程式碼
列印:
(lldb) po protocol
<Protocol: 0x6080000a84c0>
複製程式碼
protocol_addMethodDescription
/**
* Adds a method to a protocol. The protocol must be under construction.
*
* @param proto The protocol to add a method to.
* @param name The name of the method to add.
* @param types A C string that represents the method signature.
* @param isRequiredMethod YES if the method is not an optional method.
* @param isInstanceMethod YES if the method is an instance method.
*/
// 給沒有建立的協議新增指定的方法,已經註冊的協議無法新增
OBJC_EXPORT void
protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name,
const char * _Nullable types,
BOOL isRequiredMethod, BOOL isInstanceMethod)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
Protocol *protocolB = objc_allocateProtocol("PersonDelegate");
protocol_addMethodDescription(protocolB, @selector(runtimeTestAction3), "v@:", NO, YES);
unsigned int outCount;
struct objc_method_description *descList = protocol_copyMethodDescriptionList(protocolB, NO, YES, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
struct objc_method_description desc = descList[i];
char *types = desc.types;
NSLog(@"name: %@, type: %s", NSStringFromSelector(desc.name), types);
}
複製程式碼
列印:
TestProject[29062:1027253] name: runtimeTestAction3, type: v@:
複製程式碼
protocol_addProtocol
/**
* Adds an incorporated protocol to another protocol. The protocol being
* added to must still be under construction, while the additional protocol
* must be already constructed.
*
* @param proto The protocol you want to add to, it must be under construction.
* @param addition The protocol you want to incorporate into \e proto, it must be registered.
*/
// 新增一個已註冊的協議到未註冊的協議當中
proto:未註冊的協議
addition:被新增的已註冊協議
OBJC_EXPORT void
protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
// 宣告一個協議返回協議的例項
Protocol *protocol = objc_allocateProtocol("ProjectDelegate");
objc_registerProtocol(protocol); // 協議方法已經註冊
Protocol *testProtocol = objc_allocateProtocol("TestProtocol");
protocol_addProtocol(testProtocol, protocol);
// 列印這個協議遵循的協議組
unsigned int count;
Protocol **protocolList = protocol_copyProtocolList(testProtocol, &count);
for (unsigned int i = 0; i < count; i++) {
Protocol *protocolC = protocolList[i];
NSLog(@"name --- %s", protocol_getName(protocolC));
}
複製程式碼
列印:
TestProject[32945:1206976] name --- ProjectDelegate
複製程式碼
protocol_addProperty
/**
* Adds a property to a protocol. The protocol must be under construction.
*
* @param proto The protocol to add a property to.
* @param name The name of the property.
* @param attributes An array of property attributes.
* @param attributeCount The number of attributes in \e attributes.
* @param isRequiredProperty YES if the property (accessor methods) is not optional.
* @param isInstanceProperty YES if the property (accessor methods) are instance methods.
* This is the only case allowed fo a property, as a result, setting this to NO will
* not add the property to the protocol at all.
*/
//
OBJC_EXPORT void
protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name,
const objc_property_attribute_t * _Nullable attributes,
unsigned int attributeCount,
BOOL isRequiredProperty, BOOL isInstanceProperty)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
// 建立新的協議
Protocol *newProtocol = objc_allocateProtocol("DataDelegate");
// 給一個協議新增一個屬性
objc_property_attribute_t attr_T = {"T", "@\"NSString\""}; // @encod
objc_property_attribute_t attr_N = {"N", ""}; // 原子性
objc_property_attribute_t attr_C = {"C", ""}; // copy
objc_property_attribute_t attr_V = {"V", "_age"}; // 例項變數
objc_property_attribute_t attrs[] = {attr_T, attr_N, attr_C, attr_V};
protocol_addProperty(newProtocol, "age", attrs, 4, YES, YES);
// 列印協議的屬性
unsigned int outCount;
objc_property_t *propertyList = protocol_copyPropertyList(newProtocol, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
objc_property_t property = propertyList[i];
unsigned propertyCount;
objc_property_attribute_t *attrList = property_copyAttributeList(property, &propertyCount);
for (unsigned int j = 0; j < propertyCount; j++) {
objc_property_attribute_t attr = attrList[j];
const char *name = attr.name;
const char *value = attr.value;
NSLog(@"name: %s, value: %s", name, value);
}
}
複製程式碼
列印:
TestProject[33846:1268389] name: T, value: @"NSString"
TestProject[33846:1268389] name: N, value:
TestProject[33846:1268389] name: C, value:
TestProject[33846:1268389] name: V, value: _age
複製程式碼
objc_method_description
結構:
/// Defines a method
struct objc_method_description {
SEL _Nullable name; /**< The name of the method */
char * _Nullable types; /**< The types of the method arguments */
};
複製程式碼
protocol_getMethodDescription
/**
* Returns a method description structure for a specified method of a given protocol.
*
* @param p A protocol.
* @param aSel A selector.
* @param isRequiredMethod A Boolean value that indicates whether aSel is a required method.
* @param isInstanceMethod A Boolean value that indicates whether aSel is an instance method.
*
* @return An \c objc_method_description structure that describes the method specified by \e aSel,
* \e isRequiredMethod, and \e isInstanceMethod for the protocol \e p.
* If the protocol does not contain the specified method, returns an \c objc_method_description structure
* with the value \c {NULL, \c NULL}.
*
* @note This function recursively searches any protocols that this protocol conforms to.
*/
// 返回協議中指定方法的描述
OBJC_EXPORT struct objc_method_description
protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel,
BOOL isRequiredMethod, BOOL isInstanceMethod)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
struct objc_method_description desc = protocol_getMethodDescription(protocolA, @selector(numberOfSectionsInTableView:), NO, YES);
複製程式碼
列印:
(lldb) po desc.name
"numberOfSectionsInTableView:"
(lldb) po desc.types
"q24@0:8@16"
複製程式碼
protocol_copyMethodDescriptionList
/**
* Returns an array of method descriptions of methods meeting a given specification for a given protocol.
*
* @param p A protocol.
* @param isRequiredMethod A Boolean value that indicates whether returned methods should
* be required methods (pass YES to specify required methods).
* @param isInstanceMethod A Boolean value that indicates whether returned methods should
* be instance methods (pass YES to specify instance methods).
* @param outCount Upon return, contains the number of method description structures in the returned array.
*
* @return A C array of \c objc_method_description structures containing the names and types of \e p's methods
* specified by \e isRequiredMethod and \e isInstanceMethod. The array contains \c *outCount pointers followed
* by a \c NULL terminator. You must free the list with \c free().
* If the protocol declares no methods that meet the specification, \c NULL is returned and \c *outCount is 0.
*
* @note Methods in other protocols adopted by this protocol are not included.
*/
// 得到方法描述列表
OBJC_EXPORT struct objc_method_description * _Nullable
protocol_copyMethodDescriptionList(Protocol * _Nonnull proto,
BOOL isRequiredMethod,
BOOL isInstanceMethod,
unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int outCount;
struct objc_method_description *descList = protocol_copyMethodDescriptionList(protocolA, YES, YES, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
struct objc_method_description desc = descList[i];
char *types = desc.types;
NSLog(@"name: %@, type: %s", NSStringFromSelector(desc.name), types);
}
複製程式碼
列印:
TestProject[25150:681145] name: tableView:numberOfRowsInSection:, type: q32@0:8@16q24
TestProject[25150:681145] name: tableView:cellForRowAtIndexPath:, type: @32@0:8@16@24
複製程式碼
objc_property_t
定義:
/// An opaque type that represents an Objective-C declared property.
typedef struct objc_property *objc_property_t;
複製程式碼
class_getProperty
/**
* Returns a property with a given name of a given class.
*
* @param cls The class you want to inspect.
* @param name The name of the property you want to inspect.
*
* @return A pointer of type \c objc_property_t describing the property, or
* \c NULL if the class does not declare a property with that name,
* or \c NULL if \e cls is \c Nil.
*/
// 返回一個屬性,通過引數類,和名字
OBJC_EXPORT objc_property_t _Nullable
class_getProperty(Class _Nullable cls, const char * _Nonnull name)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
objc_property_t property_t = class_getProperty([Person class], "age");
const char *name = property_getName(property_t);
NSLog(@"%s", name);
複製程式碼
列印:
TestProject[20119:217782] age
複製程式碼
class_copyPropertyList
/**
* Describes the properties declared by a class.
*
* @param cls The class you want to inspect.
* @param outCount On return, contains the length of the returned array.
* If \e outCount is \c NULL, the length is not returned.
*
* @return An array of pointers of type \c objc_property_t describing the properties
* declared by the class. Any properties declared by superclasses are not included.
* The array contains \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
*
* If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0.
*/
// 得到類屬性列表
OBJC_EXPORT objc_property_t _Nonnull * _Nullable
class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int propertyCount;
objc_property_t *propertyList = class_copyPropertyList([Person class], &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++) {
objc_property_t property_t = propertyList[i];
NSLog(@"%s", property_getName(property_t));
}
free(propertyList);
複製程式碼
列印:
TestProject[20198:223479] manTest
TestProject[20198:223479] age
複製程式碼
class_getIvarLayout
/**
* Returns a description of the \c Ivar layout for a given class.
*
* @param cls The class to inspect.
*
* @return A description of the \c Ivar layout for \e cls.
*/
// 返回一個類的修飾屬性
OBJC_EXPORT const uint8_t * _Nullable
class_getIvarLayout(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
const uint8_t *array_s = class_getIvarLayout([Person class]);
複製程式碼
列印:
(lldb) po array_s
"\x01"
複製程式碼
class_getWeakIvarLayout
/**
* Returns a description of the layout of weak Ivars for a given class.
*
* @param cls The class to inspect.
*
* @return A description of the layout of the weak \c Ivars for \e cls.
*/
// 待續
OBJC_EXPORT const uint8_t * _Nullable
class_getWeakIvarLayout(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
複製程式碼
列印:
複製程式碼
property_getName
/* Working with Properties */
/**
* Returns the name of a property.
*
* @param property The property you want to inquire about.
*
* @return A C string containing the property's name.
*/
// 返回屬性的名字
OBJC_EXPORT const char * _Nonnull
property_getName(objc_property_t _Nonnull property)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
objc_property_t property_t = class_getProperty([Person class], "age");
const char *name = property_getName(property_t);
NSLog(@"%s", name);
複製程式碼
列印:
TestProject[21844:408300] age
複製程式碼
property_getAttributes
/**
* Returns the attribute string of a property.
*
* @param property A property.
*
* @return A C string containing the property's attributes.
*
* @note The format of the attribute string is described in Declared Properties in Objective-C Runtime Programming Guide.
*/
// 返回屬性的屬性字串
OBJC_EXPORT const char * _Nullable
property_getAttributes(objc_property_t _Nonnull property)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
id lenderClass = objc_getClass("Person");
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
}
複製程式碼
列印:
manTest T@"NSString",&,N,V_manTest
age Tq,N,V_age
複製程式碼
列印解釋:
objc_property_attribute_t
結構:
/// Defines a property attribute
typedef struct {
const char * _Nonnull name; /**< The name of the attribute */
const char * _Nonnull value; /**< The value of the attribute (usually empty) */
} objc_property_attribute_t;
複製程式碼
property_copyAttributeList
/**
* Returns an array of property attributes for a property.
*
* @param property The property whose attributes you want copied.
* @param outCount The number of attributes returned in the array.
*
* @return An array of property attributes; must be free'd() by the caller.
*/
// 返回屬性的屬性列表值
OBJC_EXPORT objc_property_attribute_t * _Nullable
property_copyAttributeList(objc_property_t _Nonnull property,
unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
id lenderClass = objc_getClass("Person");
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
unsigned int attrCount = 0;
objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
for (unsigned int j = 0; j < attrCount; j++) {
objc_property_attribute_t attr = attrs[j];
const char *name = attr.name;
const char *value = attr.value;
NSLog(@"屬性的描述: %s 值: %s", name, value);
}
}
複製程式碼
列印:
manTest T@"NSString",&,N,V_manTest
TestProject[23001:481165] 屬性的描述: T 值: @"NSString"
TestProject[23001:481165] 屬性的描述: & 值:
TestProject[23001:481165] 屬性的描述: N 值:
TestProject[23001:481165] 屬性的描述: V 值: _manTest
age Tq,N,V_age
TestProject[23001:481165] 屬性的描述: T 值: q
TestProject[23001:481165] 屬性的描述: N 值:
TestProject[23001:481165] 屬性的描述: V 值: _age
複製程式碼
/**
* Returns the value of a property attribute given the attribute name.
*
* @param property The property whose attribute value you are interested in.
* @param attributeName C string representing the attribute name.
*
* @return The value string of the attribute \e attributeName if it exists in
* \e property, \c nil otherwise.
*/
// 通過屬性的名字得到屬性的值
OBJC_EXPORT char * _Nullable
property_copyAttributeValue(objc_property_t _Nonnull property,
const char * _Nonnull attributeName)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
id lenderClass = objc_getClass("Person");
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
unsigned int attrCount = 0;
objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
for (unsigned int j = 0; j < attrCount; j++) {
objc_property_attribute_t attr = attrs[j];
const char *name = attr.name;
const char *value = attr.value;
const char *test = property_copyAttributeValue(property, name);
NSLog(@"屬性的描述: %s 值: %s", name, value);
NSLog(@"test -- %s", test);
}
}
複製程式碼
列印:
manTest T@"NSString",&,N,V_manTest
TestProject[23099:496137] 屬性的描述: T 值: @"NSString"
TestProject[23099:496137] test -- @"NSString"
TestProject[23099:496137] 屬性的描述: & 值:
TestProject[23099:496137] test --
TestProject[23099:496137] 屬性的描述: N 值:
TestProject[23099:496137] test --
TestProject[23099:496137] 屬性的描述: V 值: _manTest
TestProject[23099:496137] test -- _manTest
age Tq,N,V_age
TestProject[23099:496137] 屬性的描述: T 值: q
TestProject[23099:496137] test -- q
TestProject[23099:496137] 屬性的描述: N 值:
TestProject[23099:496137] test --
TestProject[23099:496137] 屬性的描述: V 值: _age
TestProject[23099:496137] test -- _age
複製程式碼
動態庫
objc_copyImageNames
/* Working with Libraries */
/**
* Returns the names of all the loaded Objective-C frameworks and dynamic
* libraries.
*
* @param outCount The number of names returned.
*
* @return An array of C strings of names. Must be free()'d by caller.
*/
// 返回所有已載入的 Objective-C 框架和動態庫的名稱
OBJC_EXPORT const char * _Nonnull * _Nonnull
objc_copyImageNames(unsigned int * _Nullable outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
unsigned int outCount;
const char **nameList = objc_copyImageNames(&outCount);
for (unsigned int i = 0; i < outCount; i++) {
const char *name = nameList[i];
NSLog(@"name : %s", name);
}
free(nameList);
複製程式碼
列印:
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection/libdispatch.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libxpc.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_trace.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_network.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libobjc.A.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libate.dylib
複製程式碼
class_getImageName
/**
* Returns the dynamic library name a class originated from.
*
* @param cls The class you are inquiring about.
*
* @return The name of the library containing this class.
*/
// 返回這個類 源動態庫名稱
OBJC_EXPORT const char * _Nullable
class_getImageName(Class _Nullable cls)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
const char *libraryName = class_getImageName([UIViewController class]);
NSLog(@"name: %s", libraryName);
複製程式碼
列印:
TestProject[34267:1284976] name: /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit
複製程式碼
objc_copyClassNamesForImage
複製程式碼
程式碼:
unsigned int outCount;
const char **nameList = objc_copyClassNamesForImage("/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit", &outCount);
for (unsigned i = 0; i < outCount; i++) {
const char *name = nameList[i];
NSLog(@"name: %s", name);
}
複製程式碼
列印:
部分資料:
TestProject[34509:1304491] name: _UIPreviewPresentationPlatterView
TestProject[34509:1304491] name: UIKeyboardUISettings
TestProject[34509:1304491] name: _UIPickerViewTopFrame
TestProject[34509:1304491] name: _UIOnePartImageView
TestProject[34509:1304491] name: _UIPickerViewSelectionBar
TestProject[34509:1304491] name: _UIPickerWheelView
TestProject[34509:1304491] name: _UIPickerViewTestParameters
TestProject[34509:1304491] name: UIPickerView
複製程式碼
objc_setAssociatedObject
/**
* 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 _Nonnull object, const void * _Nonnull key,
id _Nullable value, objc_AssociationPolicy policy)
OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
複製程式碼
程式碼:
UIImage+AddProperty.h
#import <UIKit/UIKit.h>
@interface UIImage (AddProperty)
@property(nonatomic,strong)NSString *imageWidth;
@end
UIImage+AddProperty.m
#import "UIImage+AddProperty.h"
#import <objc/runtime.h>
@implementation UIImage (AddProperty)
-(NSString *)imageWidth{
return objc_getAssociatedObject(self, _cmd);
}
-(void)setImageWidth:(NSString *)imageWidth{
objc_setAssociatedObject(self, @selector(imageWidth), imageWidth, OBJC_ASSOCIATION_ASSIGN);
}
@end
複製程式碼
objc_getAssociatedObject
/**
* 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 _Nullable
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
複製程式碼
objc_removeAssociatedObjects
/**
* 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 _Nonnull object)
OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);
複製程式碼