cool hit counter iosCategory_Intefrankly


Category Introduction

Category is a language feature added after Objective-C 2.0. The main purpose of category is to add methods to classes that already exist. In addition to this, apple recommends two other scenarios for the use of category1

  • It is possible to separate the implementation of the classes inside several different files. There are several obvious benefits to doing so. a) the size of individual files can be reduced b) Different functions can be organized into different categories c) It is possible to have multiple developers working on a class d) You can load the desired category on demand, etc.
  • Declaring private methods

Several other scenarios for the use of category.

  • Simulating Multiple Inheritance
  • Make the framework's private methods public
Category True Face
typedef struct category_t {
         const char *name;//name of the class Name of the main class
         classref_t cls;//class
         struct method_list_t *instanceMethods;//List of instance methods
         struct method_list_t *classMethods;//List of class methods
struct protocol_list_t *protocols;// list of all protocols
         struct property_list_t *instanceProperties;//all properties added
    } category_t;

The definition of category also shows that category can add instance methods, class methods, and even implement protocols and add properties. Unable to add instance variables.

Process. 1.At compile time, a structure method_list_t is generated for the methods implemented in the category, a structure property_list_t is generated for the declared properties, and a structure category_t is generated from these structures. 2.At runtime, Runtime will get the structure category_t that we saved at compile time 3.Then add the list of instance methods, the list of protocols, and the list of properties in the structure category_t to the main class 4.Add the list of class methods and protocols in the structure category_t to the metaClass of the main class

There are two things to note. 1), the methods of category do not "completely replace" the methods of the original class, that is, if category and the original class have methodA, then after the category is attached, there will be two methodA in the method list of the class 2) The methods of category are put in front of the new method list, while the methods of the original class are put behind the new method list, which is what we usually call the methods of category will "override" the methods of the original class with the same name, because the runtime is looking for methods in the order of the method list, it will stop as soon as it finds the method with the corresponding name, not knowing that there may be methods with the same name behind.

Category Why can't you add instance variables

With the structure category_t , we will know that in Category we can add instance methods, class methods, protocols, properties. The absence of the objc_ivar_list structure here means we can't add instance variables to the categories. Because at runtime, the memory layout of the object is already determined, adding instance variables would break the internal layout of the class, and this is the fundamental reason why you can't add instance variables in Category.

Why is it possible to add properties again using Runtime?

Properties can be added to categories using the associated objects in Runtime technology. Let's start with the AssociationsManager code as follows.

class AssociationsManager {
        static OSSpinLock _lock;
        static AssociationsHashMap *_map;               // associative references:  object pointer -> PtrPtrHashMap.
        AssociationsManager()   { OSSpinLockLock(&_lock); }
        ~AssociationsManager()  { OSSpinLockUnlock(&_lock); }
        AssociationsHashMap &associations() {
            if (_map == NULL)
                _map = new AssociationsHashMap();
            return *_map;

The reason for this is that the association objects are managed by the AssociationsManager, which is a static AssociationsHashMap that stores all the association objects inside. This is equivalent to having all the objects associated with the object inside a global map. The key of the map is the pointer address of this object, and the value of the map is another AssociationsHashMap, which holds the key/value pairs of the associated objects.

Is there a load method in Category? When is the load method called? Can the load method be inherited? Category has the load method, and the load method is called when the program starts loading class information. The load method can be inherited. The parent class's load method is called before the child class's load method is called

void call_load_methods(void) {
     // Has it been entered
    static bool loading = NO;
     // Is there an associated Category
    bool more_categories;
     // Since loading is a global static boolean, if the method has already been entered then it simply exits
    if (loading) return;
    loading = YES;
     // Declare an autoreleasePool object
     // The purpose of the push operation is to create a new autoreleasePool object
    void *pool = objc_autoreleasePoolPush();
    do {
         // Call the load method repeatedly until there are no
        while (loadable_classes_used > 0) {
             call_class_loads();     // First call the load method of the class
         // Call the load method in Category
        more_categories = call_category_loads();
         // Continue calling until all classes are complete
    } while (loadable_classes_used > 0  ||  more_categories);
     // Release the created autoreleasePool object
     // Change the global marker to indicate that it has been entered
    loading = NO;

1、Kyoto Tower High Altitude VRShow 100m VR Bungee and Zipline Inventory of Fancy VR Extreme Sports Bringing Speed and Passion
2、RFC2616 read pdf
3、Super Simple MySQL MasterSlave Replication Configuration Steps
4、Industry OpenAI proposes new metalearning method EPG adjusts loss function for fast training on new tasks
5、WeChat jump a small game hang analysis

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送