📜 ⬆️ ⬇️

Runtime Magic: Inspect Objective-C Objects

Objective-C, in contrast to other languages, the type of the pointer of objects does not really matter at run time when sending messages and is used only at compile time. Just run and check:
NSNumber *string = [[NSString alloc] initWithString: @"hello"]; NSLog(@"%@", string); 


You did not doubt ;-) We could use any type or even id .
During execution, all the necessary information is obtained from the class object, through the isa variable. The class object, with the class type (or simply class) is a singleton, and therefore the classes of objects can be compared by simply comparing pointers:
 [object class] == [object2 class]; 


If you collect all the information from the class object, then it is easy to restore the interfaces of objects during execution.

')

Objects


 // : - (Class) class; + (Class) class; // : - (Class) superclass; + (Class) superclass; //  : Protocol ** class_copyProtocolList(Class cls, unsigned int *outCount); //  : Ivar * class_copyIvarList(Class cls, unsigned int *outCount); //  : Method * class_copyMethodList(Class cls, unsigned int *outCount); //  : objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount); 


Variables


 // : const char * ivar_getName(Ivar ivar); // : const char * ivar_getTypeEncoding(Ivar ivar); 


Methods


 // : SEL method_getName(Method method); //       sel_getName(method_getName(method)); //  : unsigned method_getNumberOfArguments(Method method); //     index: char * method_copyArgumentType(Method method, unsigned int index); void method_getArgumentType(Method method, unsigned int index, char *dst, size_t dst_len); // : char * method_copyReturnType(Method method); void method_getReturnType(Method method, char *dst, size_t dst_len); 


Properties


Name, Type and Attributes:
const char * property_getAttributes (objc_property_t property) ;

Lines of objects


In Class , Protocol , SEL, you can ask the line with your name:
 NSString * NSStringFromClass (Class aClass); SString * NSStringFromProtocol (Protocol *proto); NSString *NSStringFromSelector (SEL aSelector); 


Alas, Ivar, Method, objc_property_t does not have its own functions and, therefore, I had to write them:
 NSString *OIStringFromIvar (Ivar ivar); NSString *OIStringFromMethod (Method method); NSString *OIStringFromProperty (objc_property_t property); 


Unfortunately, not all information is available. When compiling lost:
#define , typedef , object types and parameter names in method declarations.

The type of objects in the style: @ "NSObject" is an undocumented property of objc runtime.

If you declared an interface:
 @interface MyObject : NSObject { NSNumber *number; } - (void) setNumber: (NSNumber *) number; - (NSNumber *) number; @end 


then functions:
 OIStringFromIvar (ivar); // - (void) setNumber: (NSNumber *) number; OIStringFromMethod (method); // - (NSNumber *) number; OIStringFromMethod (method); 


will return accordingly:
 NSNumber *number - (void) setNumber: (id) arg1 - (id) arg1 


Links


Object inspector
Objective-C Runtime Reference
Objective-C Runtime Programming Guide

Source: https://habr.com/ru/post/120662/


All Articles