iOS recognizes the Load method

Source: Internet
Author: User

-----realize that the load method is different from other system methods when learning method Swilzzing recently. A Uiviewcontroller catagory was created and the Load method was re-written.

In this article , it is stated that:

+ load, as a method in Objective-c, differs greatly from other methods. Just a hook method that is called when the entire file is loaded into the runtime, before the main function call is OBJC run. There are a few key words:

    • File just loaded

    • Before the main function

    • Hook method

The full text answers two questions here to make a study summary

How is the Q:load method called?

A: When the Objective-c runtime is initialized, a callback is made by Dyld_register_image_state_change_handler each time a new mirror is added to the runtime. Execution Load_images adds all files containing the Load method to the list loadable_classes, and then finds the implementation of the corresponding Load method from this list, calling the Load method.

// after the system kernel is ready for the program, it is left to Dyld (the dynamic Link Editor's abbreviation, which is the Apple Dynamics Linker) for the rest of the work.

1. _objc_init registered callbacks first at the time of the entire runtime initializationDyld_register_image_state_change_handler (dyld_image_state_dependents_initialized,0/*Not Batch*/, &load_images);
//2. Whenever a new image is loaded into runtime, the Load_images method is executed to callback and the information list of the latest mirror is passed in InfolistConst Char*Load_images (enumdyld_image_states State, uint32_t Infocount,Const structDyld_image_info infolist[]) { BOOLfound; Found=false; for(Uint32_t i =0; i < Infocount; i++) { if(Hasloadmethods (ConstHeadertype *) (infolist[i].imageloadaddress)) {//3. If the + load symbol was found during the scanning of the imageFound =true; Break; } }
if(!found)returnNil; recursive_mutex_locker_tLock(Loadmethodlock); {rwlock_writer_t Lock2 (runtimelock);
4, will enter Load_images_nolock to find the Load method
Found=Load_images_nolock (state, Infocount, infolist); } if(found) {
///7, after the image is loaded into the runtime and ready for the Load method, execution Call_load_methods starts calling the Load method call_load_methods (); } returnNil;}
//4. Call Load_image_nolockBOOLLoad_images_nolock (enumdyld_image_states state,uint32_t Infocount,Const structDyld_image_info infolist[] {BOOLFound =NO;    uint32_t i; I=Infocount;  while(i--) {        ConstHeadertype *MHDR = (headertype*) infolist[i].imageloadaddress; if(!hasloadmethods (MHDR))Continue;
//5, call Prepare_load_methods to prepare the call to the Load method prepare_load_methods (MHDR); Found=YES; } returnfound;}//5. Call Prepare_load_methods (Add the class that needs to call the Load method to a list)voidPrepare_load_methods (ConstHeadertype *MHDR) {size_t count, I; Runtimelock.assertwriting ();
/ * Get a list of all classes via _getobjc2nonlazyclasslist * /classref_t*classlist =_getobjc2nonlazyclasslist (MHDR,&count); for(i =0; I < count; i++) {
// 6, call Schedule_class_load recursively arrange the current class and do not call the load of the parent class to enter the list
/* get a pointer to a class by Remapclass*/
Schedule_class_load (Remapclass (classlist[i));
}
/ * Same Treatment classification */
category_t**categorylist = _getobjc2nonlazycategorylist (MHDR, &count);
for(i =0; I < count; i++) {
category_t*cat =Categorylist[i];
Class CLS= Remapclass (cat->CLS);
if(!CLS)Continue;//Category for ignored Weak-linked class
Realizeclass (CLS);
assert (CLS->isa ()isrealized ());
add_category_to_loadable_list (CAT);
}
}
//6. Call Schedule_class_load (red code guarantees that the parent class calls the Load method in front of the child class) Static voidSchedule_class_load (class CLS) {if(!CLS)return; ASSERT (CLS-isrealized ()); if(Cls->data ()->flags & rw_loaded)return;
/ * First add the parent class to the list to be loaded * / schedule_class_load (CLS - superclass);
/ * And then execute add_class_to_loadabel_list to add the current class. * / add_class_to_loadable_list (CLS); CLS -SetInfo (rw_loaded);}//7. Call Call_load_methodsvoidCall_load_methods (void) { ... Do { while(Loadable_classes_used >0) {call_class_loads (); } more_categories=call_category_loads (); } while(Loadable_classes_used >0||more_categories); ...}
//WhereCall_class_loads will look for the corresponding class from a list of classes to be loaded, and then find the implementation of the @selector (load) and execute it. Static voidCall_class_loads (void) {    inti; structLoadable_class *classes =loadable_classes; intused =loadable_classes_used; Loadable_classes=Nil; Loadable_classes_allocated=0; Loadable_classes_used=0;  for(i =0; i < used; i++) {Class cls=Classes[i].cls; load_method_t Load_method=(load_method_t) Classes[i].method; if(!CLS)Continue; (*Load_method) (CLS, sel_load);    //Real call to +[xxobject load] method } if(classes) free (classes);}

(Mirror is)

Q:load method will have to walk the original class after the category of this call order?

A:

iOS recognizes the Load method

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.