iOS Development Runtime Learning: First: Runtime introduction and runtime message mechanism

Source: Internet
Author: User

A: Runtime introduction: Also the interview must answer the part

Two: The message mechanism of runtime

#import "ViewController.h"#import<objc/message.h>#import "Person.h"/*  Summary: 1:runtime: The header file must be imported <objc/message.h> This header file has been introduced <objc/runtime.h> any method invocation essence: Send a message and send the message with runtime. OC Bottom implementation through runtime implementation validation: Method call, whether really is converted to the message mechanism runtime has a prefix, who things to use who 2: ((NSObject * (*) (ID, SEL)) (void *) objc_msgsend) ([  NSObject class], @selector (alloc)); This code can be simplified to: objc_msgsend ([NSObject class], @selector (alloc)), WHERE (NSObject * (*) (ID, SEL)) This is a function pointer, the return value type is NSObject *, the parameter is (      ID, SEL), ID represents who sent the message, SEL: function entry, what message to send Xcode6 before Apple runs using Objc_msgsend. And there are parameter hints Xcode6 Apple does not recommend us to use Runtime 3: Resolve message mechanism method hint steps Find build setting Search MSG, set to No, general configuration information is set in build setting, build phases is generally introduced into the system framework and set to participate in the compilation of. m files,. h files do not participate in compiling the final build message mechanism, the compiler does The thing is the final code that needs to be recompiled with the current code, with the Xcode compiler, Clang 4:runtime: The methods are all prefixed, who are the things who start using the scene in the development: need to use runtime, message mechanism 1. Loading Force 2. Have to use runtime message Mechanism, you can call the system API or the private method in the framework 5: The invocation process of the message mechanism: interview: How to invoke the method invocation process how to call the Eat method, object method: Class object Method List class method: List of methods in the Meta Class 1. Find 2 in the corresponding class through ISA. Registration Method Number 3. Method number to find the corresponding method 4. Find only the final function to implement the address, according to the address to the method area to invoke the corresponding Function procedure: After an object is initialized, there will be an Isa pointer to the class object (the Class object's method list), the class object will have a list of methods, the method is registered according to the list method number Method number PairThe method list, according to the method number to find the corresponding method, found only the implementation of the final function address, according to the address to the memory of the method area to call the corresponding function memory 5 Region 1. Stack 2. Heap 3. Static Zone 4. Constant Zone 5. Method area 1. Stack: Do not need to manually manage memory, automatically manage 2. Heap      , to release himself.*/@interfaceViewcontroller ()@end@implementationViewcontroller/** * 1:objc_getclass ("person") means to get the class object, Sel_registername ("alloc") means to register a Method 2: Call Eat: Is the object call eat, the message mechanism is who sent what message: objc_ Msgsend (p, @selector (eat)); */- (void) viewdidload {[Super viewdidload]; //Person *p = [Person alloc];Person *p = Objc_msgsend (Objc_getclass (" Person"), Sel_registername ("Alloc")); //p = [P init];p = objc_msgsend (P, Sel_registername ("Init")); //Call Eat//[P eat];objc_msgsend (p, @selector (Eat));//objc_msgsend (P, @selector (run:), +);       }/** 1: An object's initialization method can be decomposed into two steps: Alloc to open up memory space, init to initialize: id OBJC = [nsobject alloc]; When a memory space is opened to obtain a class object, the class object is initialized to get an object OBJC = [OBJC     INIT];        2: The above initialization process is implemented with Runtime://id OBJC = [NSObject alloc];        ID OBJC = Objc_msgsend ([NSObject class], @selector (alloc));        OBJC = [OBJC init]; OBJC = Objc_msgsend (OBJC, @selector (init)); * */- (void) test{//id OBJC = [nsobject alloc];    IDOBJC = Objc_msgsend ([nsobjectclass], @selector (alloc)); //OBJC = [OBJC init];OBJC =objc_msgsend (OBJC, @selector (init));}@end

iOS Development Runtime Learning: First: Runtime introduction and runtime message mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.