Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge” click to see the details of the activity.

This article focuses on the basic usage of class member variables, attributes, methods, etc.

The test class

Write a test class like this:

@interface Test() { int s; } @property(nonatomic, strong)NSString * STR; @end@implementation Test - (NSString *) STR {// method return @" STR "; } - (void)setStr:(NSString *) STR {} - (void)test {// method printf("test in main\n"); } + (void)test2022 {} // class method @endCopy the code

Gets a class member variable

Use the Runtime API class_copyIvarList to get a list of class member variables. The Test class has only one member variable, s. The STR defined by property does not produce a member variable, _str, because it implements get and set.

unsigned int ivarCount;
Class class =  Test.class;
Ivar *ivarList = class_copyIvarList(class, &ivarCount);
for (unsigned int i = 0; i < ivarCount; i++) {
    Ivar myIvar = ivarList[i];
    const char *ivarName = ivar_getName(myIvar);
    NSLog(@"%@ ivar(%d) : %s", class, i, ivarName);
}
free(ivarList);
Copy the code

Gets the attributes of the class

The Runtime API class_copyPropertyList is used to get the property list of the class. The Test class has only one property, STR.

unsigned int propertyCount;
Class class = Test.class;
objc_property_t *propertyList = class_copyPropertyList(class, &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++) {
    const char *propertyName = property_getName(propertyList[i]);
    NSLog(@"%@ propertyName(%d) : %s", class, i, propertyName);
}
free(propertyList);
Copy the code

Gets the list of methods for the class

Get the list of class methods using the Runtime API class_copyMethodList.

unsigned int methodCount;
Class class = Test.class;
Method *methodList = class_copyMethodList(class, &methodCount);
for (unsigned int i = 0; i < methodCount; i++) {
    Method method = methodList[i];
    NSLog(@"%@ method(%d) : %@", class, i, NSStringFromSelector(method_getName(method)));
}
free(methodList);
Copy the code

Gets the list of protocols for the class

The Runtime API class_copyProtocolList is used to get the protocol list of the class. Because the Test class doesn’t implement any protocol, NSObject is used.

unsigned int protocolCount;
Class class = NSObject.class;
__unsafe_unretained Protocol **protocolList = class_copyProtocolList(class, &protocolCount);
for (unsigned int i = 0; i < protocolCount; i++) {
    Protocol *myProtocal = protocolList[i];
    const char *protocolName = protocol_getName(myProtocal);
    NSLog(@"%@ protocol(%d) : %s", class, i, protocolName);
}
free(protocolList);
Copy the code

Gets the class method of the class

Get the class method of the class using the same Runtime API class_copyMethodList. Only the non-class methods are stored on the metaclass. You can find the class methods by obtaining the metaclass of the class using object_getClass.

unsigned int methodCount;
Class class = object_getClass(Test.class);
Method *methodList = class_copyMethodList(class, &methodCount);
for (unsigned int i = 0; i < methodCount; i++) {
    Method method = methodList[i];
    NSLog(@"%@ method(%d) : %@", class, i, NSStringFromSelector(method_getName(method)));
}
free(methodList);
Copy the code