In layman's language Runtime (a): acquaintance

Runtime series

Layman Runtime (a): acquaintance
layman Runtime (2): The data structure
layman Runtime (III): messaging
layman Runtime (IV): The nature of super
layman Runtime (e): a particular application
layman Runtime (VI): interview questions related

Runtime Introduction

  • Runtime with a C、汇编run library prepared, comprising the API of a number of C language, a lot of dynamic package related functions;
  • Objective-C is a dynamic language runtime, allowing many operations to further defer the program is running. OCThe dynamics is Runtimesupported and implemented, Rumtimeis its core;
  • We usually write OCthe code, the underlying are converted to Runtime APImake the call.

Objective-C is a dynamic language runtime

What is a compile-time and run-time?

  • Compilation: Compiler program code into computer language can be identified, for only a few simple syntax check;
  • Runtime: Code run up, is loaded into memory at this time if an error causes the program to crash. Such as the classic unrecognized selector send to instance/classCrash: .

Language differences with the dynamic language runtime compile time?

  • Language compilation: a function at compile time resolution;
  • Dynamic Language Runtime: The function resolutions deferred until runtime.
For example

For NSString *string = [[NSMutableArray alloc]init];

  • Compile-time: time compiler type checking, since a given NSStringtype is a pointer assignment NSMutableArrayobject, the compiler will give a warning type mismatch. But the compiler will stringas NSStringexamples, the stringobject calls the NSStringmethod, the compiler without any problems, and call NSMutableArraythe method, the compiler will direct error.
  • Runtime: Because stringactually pointing to a NSMutableArraytarget, NSMutableArraythe object is no stringByAppendingString:method, resulting in Crash: unrecognized selector send to instance.
    NSString *string = [[NSMutableArray alloc]init];  //⚠️Incompatible pointer types initializing 'NSString *' with an expression of type 'NSMutableArray *'
    [string stringByAppendingString:@"abc"];
    [string addObject:@"abc"];  //❌No visible @interface For 'NSString' declares the selector 'addObject:'

There are two versions Runtime

  • Legacy (earlier), the corresponding programming interface: Objective-C 1.0, is applied 32-bit programs on OS X desktop;
  • AND ECONOMICS (modern version), the corresponding programming interface: in Objective-C 2.0 , is applied iPhone applications and 64-bit programs on OS X v10.5 and later.

Objective-C Runtime program to interact with the system in three different levels

  • By Objective-C source code;
  • Methods Foundation framework NSObject class definition, such as:
// 根据 instance 对象或者类名获得一个 class 对象
- (Class)class
+ (Class)class
// 判断当前 instance/class 对象的 isa 指向是不是 class/meta-class 对象或者它的子类类型
- (BOOL)isKindOfClass:(Class)cls
+ (BOOL)isKindOfClass:(Class)cls
// 判断当前 instance/class 对象的 isa 指向是不是 class/meta-class 对象类型
- (BOOL)isMemberOfClass:(Class)cls
+ (BOOL)isMemberOfClass:(Class)cls
// 判断对象是否可以接收特定消息
- (BOOL)respondsToSelector:(SEL)sel
+ (BOOL)respondsToSelector:(SEL)sel
// 判断对象是否实现了特定协议中定义的方法
- (BOOL)conformsToProtocol:(Protocol *)protocol
+ (BOOL)conformsToProtocol:(Protocol *)protocol
// 可以根据一个 SEL,得到该方法的 IMP
- (IMP)methodForSelector:(SEL)sel
+ (IMP)methodForSelector:(SEL)sel

Or related

// 动态创建一对类和元类(参数:父类,类名,额外的内存空间)
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
// 注册一对类和元类(要在类注册之前添加成员变量)
void objc_registerClassPair(Class cls) 
// 销毁一对类和元类
void objc_disposeClassPair(Class cls)
// 获取 isa 指向的 Class
Class object_getClass(id obj)
// 设置 isa 指向的 Class
Class object_setClass(id obj, Class cls)
// 判断一个 OC 对象是否为 Class
BOOL object_isClass(id obj)
// 判断一个 Class 是否为元类
BOOL class_isMetaClass(Class cls)
// 获取父类
Class class_getSuperclass(Class cls)

Member variable

// 获取一个实例变量信息
Ivar class_getInstanceVariable(Class cls, const char *name)
// 拷贝实例变量列表(最后需要调用 free 释放)
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
// 设置和获取成员变量的值
void object_setIvar(id obj, Ivar ivar, id value)
id object_getIvar(id obj, Ivar ivar)
// 动态添加成员变量(已经注册的类是不能动态添加成员变量的)
BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)
// 获取成员变量的相关信息
const char *ivar_getName(Ivar v)
const char *ivar_getTypeEncoding(Ivar v)

Property related

// 获取一个属性
objc_property_t class_getProperty(Class cls, const char *name)
// 拷贝属性列表(最后需要调用 free 释放)
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
// 动态添加属性
BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
// 动态替换属性
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
// 获取属性的一些信息
const char *property_getName(objc_property_t property)
const char *property_getAttributes(objc_property_t property)

RELATED

// 获得一个实例方法、类方法
Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)
// 方法实现相关操作
IMP class_getMethodImplementation(Class cls, SEL name) 
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2) 
// 拷贝方法列表(最后需要调用 free 释放)
Method *class_copyMethodList(Class cls, unsigned int *outCount)
// 动态添加方法
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
// 动态替换方法
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
// 获取方法的相关信息(带有 copy 的需要调用 free 去释放)
SEL method_getName(Method m)
IMP method_getImplementation(Method m)
const char *method_getTypeEncoding(Method m)
unsigned int method_getNumberOfArguments(Method m)
char *method_copyReturnType(Method m)
char *method_copyArgumentType(Method m, unsigned int index)
// 选择器相关
const char *sel_getName(SEL sel)
SEL sel_registerName(const char *str)
// 用 block 作为方法实现
IMP imp_implementationWithBlock(id block)
id imp_getBlock(IMP anImp)
BOOL imp_removeBlock(IMP anImp)

Related objects associated
Portal: OC - Association associated objects

// 添加关联对象
void objc_setAssociatedObject(id object, const void * key, id value, objc_AssociationPolicy policy)
// 获得关联对象
id objc_getAssociatedObject(id object, const void * key)
// 移除指定 object 的所有关联对象
void objc_removeAssociatedObjects(id object)

What are Runtime application?

  • Using associated object (the AssociatedObject) to add an attribute classification
  • All members of the class variable traverse (modified textfield placeholder text color, and turn the dictionary model, automatic archiving solution file)
  • Implemented method of switching (switching system intercept method)
  • Message forwarding mechanism using the method to solve unusual problems can not be found

Related Links

Apple maintained Runtime open source
GNU open source maintained Runtime
Apple's official documentation Runtime

Published 12 original articles · won praise 0 · Views 196

Guess you like

Origin blog.csdn.net/weixin_42350379/article/details/104485443