4, iOS underlying Analysis - Analysis of the structure of the class

class

Defined generally known

1, on the class

Class designed just focus on three things: class name, attributes and methods

Objects have the same attributes and behavior can be abstracted as a class. The class name is an identifier required to meet specifications, usually the first letter of the class name uppercase, and can not be underlined, if there is more than one word use hump principles. In the division of class methods, practices in general, who is most familiar with this method then put this method is allocated to whom. In OC, the object method calls to the called message mechanism, namely the established object sends what messages.

2, simple memory analysis

Class creates objects, each in memory occupies some storage space, each object has a copy of their own individual member variables, member methods all objects public classes, methods, only one in the entire memory, the class itself occupies a memory space in memory, the method of storing this class.

Each internal default object has a pointer to the class object isa used.

[P eat]; p represents the object pointed to send a message eat, eat method call object, and the object will be found in the class of the method of storing the pointer along the inside isa performed.

isa is a hidden object pointer to create the object's class.

 

Analysis of the structure of the underlying class

1, the structure of the class

The first to look at the source code by the structure of the class in the end is what kind of set up.

struct objc_class : objc_object {
    // Class ISA;  //8 占用的空间,后面进行内存偏移读取bits的时候要用
    Class superclass; //8
    cache_t cache;      //16       // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags

    class_rw_t *data() { 
        return bits.data();
    }
下边是一些类似 setData 的方法就是省略了
    void setData(class_rw_t *newData) {
        bits.setData(newData);
    }
。。。。。。。

We know that reading the source code, there are four class property.

  1. Class isa

          isa hidden attribute, isa points to the class metaclass (meta class) inherited from the parent class objc_object.

          isa pointer is associated object class and

struct objc_object {
    Class Nonnull isa OBJC_ISA_AVAILABILITY;
}

    2 . Class superclass

          Pointing to the parent class is inherited class

    3. cache_t Cache

          Can guess from the name is the cache (caching method, is behind a two-dimensional array analysis cache_t when specific analysis)

struct cache_t {
    struct bucket_t *_buckets;  //8
    mask_t _mask;               //4
    mask_t _occupied;           //4

pointer type so _buckets length 8, mask_t View source may know the type int32 4

typedef uint32_t mask_t; 
struct bucket_t {
private:
    // IMP-first is better for arm64e ptrauth and no worse for arm64.
    // SEL-first is better for armv7* and i386 and x86_64.
#if __arm64__
    MethodCacheIMP _imp;
    cache_key_t _key;
#else
    cache_key_t _key;
    MethodCacheIMP _imp;
#endif

The method can be seen that the source cache read is stored by the key value to find the corresponding method imp

4 . class_data_bits_t  bits

          Which is a structure from the overall analysis for this place is but specifically what needs to be verified to store data, analysis continues

 

Analysis exploration

1, property

(1), to create a class LGPerson, add properties and methods

The expansion of knowledge: .m method not implemented

  1.  If you do not call, there will not compile-time and run-time problems
  2. If you call, do not compile-time error, run-time crash
@interface LGPerson : NSObject{
    NSString *nickName;
}

@property (nonatomic, copy) NSString *name;

- (void)sayHello;
+ (void)sayHappy;

@end

(2), obtained by the memory offset bits

Memory alignment in accordance with the principles we use x / 4gx print to get the first 32 bytes address offset, we should be able to obtain the contents of bits, which is the theory of the address bits. Here we turn about strong (strong turn into class_data_bits_t * ), print it again, and finally print out class_data_bits_t structure

 OC generally call member variables are used -> for example: p * $ 4-> ro Sometimes there will distinguish

struct objc_class : objc_object {
    // Class ISA; // 8
    Class superclass; // 8
    cache_t cache;    // 16 不是8         // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags

    class_rw_t *data() { 
        return bits.data();
    }

(3)、class_rw_t

You can see the type of $ 7 is class_rw_t, by top print    class_rw_t come after a class_rw_t type for local storage class attributes and methods, look at the implementation of class_rw_t, returns bits.data (), what we call here the method data pointers, direct value p * $ 7, the following results

The resulting print (class_rw_t) $ 8

(4)、properties

Use the p command to view the value of the properties of the result, type property_array_t (there is defined in the source code objc-runtime-new.h in), is a two-dimensional array, continue to explore its internal list, were p $ 10.list

class property_array_t : 
    public list_array_tt<property_t, property_list_t> 
{
    typedef list_array_tt<property_t, property_list_t> Super;

 public:
    property_array_t duplicate() {
        return Super::duplicate<property_array_t>();
    }
};

(5)、property_list_t

To give property_list_t types (defined in the source code has objc-runtime-new.h) is a $ 11 , inherited from entsize_list_tt

struct property_list_t : entsize_list_tt<property_t, property_list_t, 0> {
};
struct entsize_list_tt {
    uint32_t entsizeAndFlags;
    uint32_t count;
    Element first;
下边的不重要,两个int32类型肯定不可能是存储属性的,所以继续查看first

(6)、first

View property_list_t inheritance and entsize_list_tt found first in its internal methods, try printing p $ 11-> first, finally found a property name

Described in the attribute class class_data_bits_t bits stored inside

 

2, member variables

1, class_rw_t
after finding a storage property did not see a member variable, since the property is in class_rw_t kept in. Before we should come across a case

@interface LGPerson : NSObject{
    NSString *hobby;
    NSString *_nickName;
}

@property (nonatomic, copy) NSString *nickName;

If the property (nickName) and member variables with an underscore (_nickName) of the same name, then there will only be a presence. Further attributes can self.nickName nickName and _nickName. So guess at the underlying property is certainly generated a member variable to be underlined (behind the underlying analysis will be analyzed, do not expand here).

Look at the underlying source code class_rw_t, the feeling can be set is stored in the class_ro_t * ro

struct class_rw_t {
    // Be warned that Symbolication knows the layout of this structure.
    uint32_t flags;
    uint32_t version;

    const class_ro_t *ro;

    method_array_t methods;
    property_array_t properties;
    protocol_array_t protocols;

    Class firstSubclass;
    Class nextSiblingClass;

    char *demangledName;

We can know this property and store local member variables are

class_rw_t

properties property storage,

ro                member variable storage

By name may determine methods should continue to be stored method validation

 

3, a method of storing the class

(1), examples of the method

$ 2 is class_rw_t, reading methods. Can know by looking at the source code stored in the format is returned

Method SEL typer method name corresponding to the type of method implemented imp

class method_array_t : 
    public list_array_tt<method_t, method_list_t> 
{
    typedef list_array_tt<method_t, method_list_t> Super;

省略。。。。。。。
};

struct method_t {
    SEL name;
    const char *types;
    MethodListIMP imp;
。。。。。。。。。
(lldb) p $2.methods
(method_array_t) $12 = {
  list_array_tt<method_t, method_list_t> = {
     = {
      list = 0x0000000100002240
      arrayAndFlag = 4294976064
    }
  }
}
(lldb) p $12.list
(method_list_t *) $13 = 0x0000000100002240
(lldb) p *$13
(method_list_t) $14 = {
  entsize_list_tt<method_t, method_list_t, 3> = {
    entsizeAndFlags = 26
    count = 4
    first = {
      name = "sayHello"
      types = 0x0000000100001f8b "v16@0:8"
      imp = 0x0000000100001b90 (LGTest`-[LGPerson sayHello] at LGPerson.m:13)
    }
  }
}
(lldb) 

View by viewing Methods the SET instance methods and properties only after which we created, get method. Then the class methods in which it?

Re-analyze  objc_object  

1), the superclass for we call the class method when, for example: [LGPerson sayHappy]; call like this, with the name of the parent class subclass name is different, obviously can not be in the parent class.

2), Cache looked cache_t source, this place is really more related methods, but the name should not be cached.

3), bits bits we just read class_rw_t of

properties property storage,

ro                member variable storage

methods   example of a method of storing the class

4), the ISA then only isa. Earlier we know isa pointing yuan class with the same name, the name of the class call the class method is used, it is likely that the yuan class. Explore

 

(2), the method of storage class

Ibid x / 4gx isa printed address and fetch the metaclass

class_data_bits_t , ->   the Data ()       -     class_rw_t -       Methods , get list array, we finally found the definition of class methods sayHappy instance methods and processes look like the same, so a direct look at the results

(lldb) p $13.list
(method_list_t *) $14 = 0x00000001000021d8
(lldb) p *$14
(method_list_t) $15 = {
  entsize_list_tt<method_t, method_list_t, 3> = {
    entsizeAndFlags = 26
    count = 1
    first = {
      name = "sayHappy"
      types = 0x0000000100001f8b "v16@0:8"
      imp = 0x0000000100001bc0 (LGTest`+[LGPerson sayHappy] at LGPerson.m:17)
    }
  }
}

to sum up

  1. Class inheritance and isa point see below
    1. Class - parent - the base class (NSObject class) - nil
    2. Class - metaclass - metaclass of the parent class - membered the base classes (NSObject meta class)
  2. And wherein the properties of the class member variables are stored in the class class_rw_t structural body
    1. class_rw_t
      1. properties property storage,
      2. ro                member variable storage
      3. methods   instance method of class
  3. The definition of property, accompanied by automatic generation member variables as well as its getter and setter of
  4. Class method, places example method form, stored in a metaclass, and metaclass is inherited from the NSObject, forming a closed loop

Spread

pointer

  • int a; // This is a common整型变量

  • int *p;// First, from Pthe beginning, with the first *bound, the description Pis a 指针, then the intcombination described pointer points to the type of content int型, it Pis a return整型数据的指针

 

Published 83 original articles · won praise 12 · views 180 000 +

Guess you like

Origin blog.csdn.net/shengdaVolleyball/article/details/104033586