The Runtime member variables, attributes, and method-related functions of the previous article are basically not used by themselves, and need to be combined with some class_ starting functions.

Now, let’s look at these class_ functions, some of which are used in conjunction with the functions in the previous article.

The function description

1. class_getName

OBJC_EXPORT const char * _Nonnull
class_getName(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Takes an argument of type Class and returns its name

2. class_isMetaClass

OBJC_EXPORT BOOL
class_isMetaClass(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Returns a Boolean value to determine whether CLS is a metaclass. An introduction to metaclasses

3. class_getSuperclass

OBJC_EXPORT Class _Nullable
class_getSuperclass(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Gets the type of the CLS parent class.

4. class_setSuperclassabandoned

Don’t make an introduction

5. class_getVersion

OBJC_EXPORT int
class_getVersion(Class _Nullable cls)
    OBJC_AVAILABLE(10.0.2.0.9.0.1.0.2.0);
Copy the code

Get the version number corresponding to the CLS

6. class_setVersion

OBJC_EXPORT void
class_setVersion(Class _Nullable cls, int version)
    OBJC_AVAILABLE(10.0.2.0.9.0.1.0.2.0);
Copy the code

Set the version number of the CLS

7. class_getInstanceSize

OBJC_EXPORT size_t
class_getInstanceSize(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Gets the memory size of the CLS. For more on class_getInstanceSize, see the difference between sizeof, class_getInstanceSize, and malloc_size

8. class_getInstanceVariable

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);
Copy the code

Gets the Ivar of the specified CLS member variable

9. class_getClassVariable

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);
Copy the code

The underlying call to class_getInstanceVariable is class_getInstanceVariable, but if anything, it’s not. A reasonable explanation

10. class_copyIvarList

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);
Copy the code

Gets all current member variables (excluding the parent class)

demo:

unsigned int outCount;
Ivar *ivarList = class_copyIvarList([CZPerson class], &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    Ivar ivar = ivarList[i];
    NSLog(@"%s".ivar_getName(ivar));
}

free(ivarList);
Copy the code

11. class_getInstanceMethod

OBJC_EXPORT Method _Nullable
class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
    OBJC_AVAILABLE(10.0.2.0.9.0.1.0.2.0);
Copy the code

Gets the instance method specified by CLS, including the parent class

12. class_getClassMethod

OBJC_EXPORT Method _Nullable
class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
    OBJC_AVAILABLE(10.0.2.0.9.0.1.0.2.0);
Copy the code

Gets the CLS specified class methods, including the parent class

13. class_getMethodImplementation

OBJC_EXPORT IMP _Nullable
class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Get the implementation of the method

14. class_getMethodImplementation_stret

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;
Copy the code

Gets the implementation of a method whose return value is a structure.

Let’s look at the implementation to make it clear

IMP class_getMethodImplementation_stret(Class cls, SEL sel)
{
    IMP imp = class_getMethodImplementation(cls, sel);

    // Translate forwarding function to struct-returning version
    if (imp == (IMP)&_objc_msgForward /* not _internal! * /) {
        return (IMP)&_objc_msgForward_stret;
    }
    return imp;
}
Copy the code

It’s also internally called class_getMethodImplementation, which returns the result as a structure

15. class_respondsToSelector

OBJC_EXPORT BOOL
class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Returns a Boolean value to determine whether CLS has sel corresponding method implementation

16. class_copyMethodList

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);
Copy the code

Get instance methods with concrete implementations (only declared methods cannot get)

17. class_conformsToProtocol

OBJC_EXPORT BOOL
class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Returns a Boolean to determine whether the CLS complies with protocol

18. class_copyProtocolList

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);
Copy the code

Get all the agreements that CLS complies with

19. class_getProperty

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);
Copy the code

Gets the specified property

20. class_copyPropertyList

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);
Copy the code

Gets all the attributes in the CLS

21. class_getIvarLayout

OBJC_EXPORT const uint8_t * _Nullable
class_getIvarLayout(Class _Nullable cls)
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Returns a strongly referenced Ivar

Including the following

{__strong id objc;
}
Copy the code

22. class_getWeakIvarLayout

OBJC_EXPORT const uint8_t * _Nullable
class_getWeakIvarLayout(Class _Nullable cls)
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Returns a weakly referenced Ivar

Including the following

{__weak id objc;
} 

Copy the code

23. class_addMethod

OBJC_EXPORT BOOL
class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, 
                const char * _Nullable types) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Adds the specified methods and implementations to the CLS. This approach replaces the implementation in the parent class, but not the implementation in this class.

24. class_replaceMethod

OBJC_EXPORT IMP _Nullable
class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, 
                    const char * _Nullable types) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Replacement method implementation, name, method name to be replaced, imp replacement implementation, types method parameters. Note the return value, which is the original method implementation.

25. class_addIvar

class_addIvar(Class _Nullable cls, const char * _Nonnull name, size_t size, 
              uint8_t alignment, const char * _Nullable types) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Add new member variables. This function can only be called after objc_allocateClassPair and before objc_registerClassPair. Adding instance variables to existing classes is not supported.

26. class_addProtocol

OBJC_EXPORT BOOL
class_addProtocol(Class _Nullable cls, Protocol * _Nonnull protocol) 
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

New protocol for CLS

27. class_addProperty

OBJC_EXPORT BOOL
class_addProperty(Class _Nullable cls, const char * _Nonnull name,
                  const objc_property_attribute_t * _Nullable attributes,
                  unsigned int attributeCount)
    OBJC_AVAILABLE(10.7.4.3.9.0.1.0.2.0);
Copy the code

Add attributes for CLS

28. class_replaceProperty

OBJC_EXPORT void
class_replaceProperty(Class _Nullable cls, const char * _Nonnull name,
                      const objc_property_attribute_t * _Nullable attributes,
                      unsigned int attributeCount)
    OBJC_AVAILABLE(10.7.4.3.9.0.1.0.2.0);
Copy the code

Replace the attribute

29. class_setIvarLayout

OBJC_EXPORT void
class_setIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Set Ivar to be strongly referenced

30. class_setWeakIvarLayout

OBJC_EXPORT void
class_setWeakIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout)
    OBJC_AVAILABLE(10.5.2.0.9.0.1.0.2.0);
Copy the code

Set Ivar weak reference

31. objc_getFutureClass

Do not call this function yourself