1、what is runtime?
The runtime system acts as a kind of operating system for the Objective-C language; it’s what makes the language work.
2、how Objective-C programs interact with the runtime system?
1、through Objective-C source code.
2、through methods defined in the NSObject class of the Foundation framework.
3、through direct calls to runtime functions.
3、Messaging
Learn what is objc_msgSend and how message convert into objc_msgSend?
The compiler converts a message expression,[receiver message] into a call on a messaging function, objc_msgSend.
objc_msgSend(receiver, selector, arg1, arg2, ...)
the key to messaging lies in the structures that the compliler builds for each class and object.Every class structure includes these two essential elements:
. A pointer to the superclass.
. A class dispatch table.
isa(its class access)
id
typedef struct objc_object *id;
struct objc_object {
Class isa OBJC_ISA_AVAILABILITY;
};
Class
typedef struct objc_class *Class;
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
if !OBJC2
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
endif
} OBJC2_UNAVAILABLE;
SEL
typedef struct objc_selector *SEL;
IMP
typedef id (*IMP)(id, SEL, ...);
Method
typedef struct objc_method *Method;
struct objc_method {
SEL method_name OBJC2_UNAVAILABLE;
char *method_types OBJC2_UNAVAILABLE;
IMP method_imp OBJC2_UNAVAILABLE;
} OBJC2_UNAVAILABLE;
4、Dynamic Method Resolution
This chapter describes how you can provide an implementation of a method dynamically.
5、Message Forwarding
The forwarding feature is a dispatch center , act as like multiple inheritance.
Forwarding not only mimics multiple inheritance, it also makes it possible to develop lightweight objects that represent or "cover" more substantial objects.The surrogate stands in for the other object and funnels messages to it.
notice:implement - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector before use the method "forwardInvovation"
6、Type Encodings
To assist the runtime system, the compiler encodes the return and argument types for each method in a character string and associates the string with the method selector. The coding scheme it uses is also useful in other contexts and so is made publicly available with the @encode()
compiler directive. When given a type specification, @encode()
returns a string encoding that type. The type can be a basic type such as an int
, a pointer, a tagged structure or union, or a class name—any type, in fact, that can be used as an argument to the C sizeof()
operator.
7、Declared Properties
When the compiler encounters property declarations (see Declared Properties in The Objective-C Programming Language), it generates descriptive metadata that is associated with the enclosing class, category or protocol. You can access this metadata using functions that support looking up a property by name on a class or protocol, obtaining the type of a property as an @encode
string, and copying a list of a property's attributes as an array of C strings. A list of declared properties is available for each class and protocol.
引用:
1、Objective-C Runtime Programming Guide
2、Objective-C runtime之运行时的基本特点(一)
3、Objective-C runtime之消息(二)
4、Objective-C runtime之消息转发机制(三)