Write in the front: the phase summary of iOS underlying principle exploration is a summary of the underlying exploration content, the chapter will not be too much, mainly for the details of the exploration summary. I hope that's helpful.Copy the code

Summary from the following column

  • Exploring the underlying principles of iOS

Start with an exam paper

Multiple choice

  • 1, inLP64Next, how many bytes does a pointer have?
  • 4 A,
  • B, 8
  • C, 16
  • D, 64
B
Copy the code
  • 2. What elements exist in the memory structure of an instance object?
  • A. Member variables
  • B, the superClass
  • C, cache_t
  • D, –
A the size of an instance object is determined by its member variables; This is not to be confused with the memory structure of the class, which contains the superClass, cache_t, and bit data.Copy the code
  • 3. Sizeof (struct3) =
struct LGStruct1 {
    char b;
    int c;
    double a;
    short d;
}

struct LGStruct2 {
    double a;
    int b;
    char c;
    short d;
}

struct LGStruct2 {
    double a;
    int b;
    char c;
    struct LGStruct1 str1;
    short d;
    int e;
    struct LGStruct2 str2;
}
Copy the code
  • A, 48
  • B, 56
  • C, 64
  • D, 72
C this is a basic problem that requires patience. I'm going to introduce a method holistic method here. In LGStruct1, you can see that there's a double of 8 bytes; Char int = 8 bytes; double = 8 bytes; short = 2 bytes; In combination with memory alignment the structure size must be an integer multiple of the internal maximum variable so it is 24 bytes; Similarly, in LGStruct2, double is 8 bytes, int 4, char 1 short 2, align 16 bytes; LGStruct3: double, int + char 8, str1 24, short + int 8, str2 16 1: Data member alignment rules: For struct (or union) data members, the first data member is placed at offset 0, and each data member is stored starting from the size of the member or the size of its children (as long as the member has children, such as arrays, Structure, etc.) (for example, if int is 4 bytes, it starts at the integer multiple address of 4. 2: Struct as members: If a structure has some struct members, the struct members are stored starting at an address that is an integer multiple of the largest internal element size. (struct a contains a char,int,double, etc.) (struct b contains a char,int,double, etc.) (struct a contains a char,int,double, etc.) (b contains a char,int,double, etc.) (b contains a char,int,double, etc.)Copy the code
  • Re1 re2 re3 re4 RE5 RE6 re7 re8
    BOOL re1 = [(id)[NSObject class] isKindOfClass:[NSObject class]];       //
    BOOL re2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]];     //
    BOOL re3 = [(id)[SMPerson class] isKindOfClass:[SMPerson class]];       //
    BOOL re4 = [(id)[SMPerson class] isMemberOfClass:[SMPerson class]];     //
    NSLog(@" re1 :%hhd\n re2 :%hhd\n re3 :%hhd\n re4 :%hhd\n",re1,re2,re3,re4);

    BOOL re5 = [(id)[NSObject alloc] isKindOfClass:[NSObject class]];       //
    BOOL re6 = [(id)[NSObject alloc] isMemberOfClass:[NSObject class]];     //
    BOOL re7 = [(id)[SMPerson alloc] isKindOfClass:[SMPerson class]];       //
    BOOL re8 = [(id)[SMPerson alloc] isMemberOfClass:[SMPerson class]];     //
    NSLog(@" re5 :%hhd\n re6 :%hhd\n re7 :%hhd\n re8 :%hhd\n",re5,re6,re7,re8);
Copy the code
  • A, 1011, 1111
  • B, 1100, 1011
  • C, 1000, 1111
  • D, 1101, 1111
+ (BOOL)isKindOfClass:(Class) CLS {for (Class TCLS = self->ISA(); tcls; tcls = tcls->getSuperclass()) { if (tcls == cls) return YES; } return NO; } + (BOOL)isMemberOfClass:(Class)cls { return self->ISA() == cls; } 1, [(id)[NSObject class] isKindOfClass:[NSObject class]]; - Get the metaclass of 'NSObject' where 'TCLS' is NSObjectMateClass and 'CLS' is NSObjectClass, so it is not equal; - 'for' loop, 'TCLS' is equal to the root metaclass's parent class NSObjectClass, which is the root class; - The second judgment in the loop is to return 'YES'; - The final result is' YES '. 2. [(ID)[NSObject Class] isMemberOfClass:[NSObject Class]]; 3, [(id)[SMPerson class] isKindOfClass:[SMPerson class]]; - Obtain the metaclass of SMperson where TCLS is SMPersonMateClass and CLS = SMPersonClass is not equal; - the for loop, TCLS = SMPersonMateClass superclass SMPersonSuperClassMateClass, and SMPersonClass unequal; - for loop TCLS = SMPersonSuperClassMateClass parent class, namely NSOBjectMateClass, and SMPersonClass unequal; - for loop TCLS = NSObjectMateClass parent class, i.e. NSObjectClass, is not equal to SMPersonClass; - for loop TCLS = NSObjectClass's parent class, which is nil; Exit the loop and return NO; 4, [(id)[SMPerson Class] isMemberOfClass:[SMPerson Class]]; 5, [(id)[SMPerson Class] isMemberOfClass:[SMPerson Class]]; - SMPersonMateClass is not equal to SMPersonClass returns NO 5, [(id)[NSObject alloc] isKindOfClass:[NSObject class]]; -nsobjectClass is equal to NSObjectClass 6. [(ID)[NSObject Alloc] isMemberOfClass:[NSObject Class]]; -nsobjectClass is equal to NSObjectClass 7, [(id)[SMPerson Alloc] isKindOfClass:[SMPerson Class]]; 8, [(id)[SMPerson alloc] isMemberOfClass:[SMPerson class]]; - SMPersonClass equals SMPersonClass returns YES; IsKindOfClass: it will first find the class or metaclass TCLS by obtaining the method caller's ISA, then make a comparison with CLS, and return YES if the same; If YES, return YES; if YES, return YES. Otherwise, return NO until NSObject's parent class is nil; IsMemberOfClass: just find the class or metaclass through the method caller's ISA, and then compare with CLS, directly return more resultsCopy the code
  • This algorithm aligns a few bytes
  • A, 7
  • B, 8
  • C, 14
  • D, 16
B if x=0001 then x+7 = 8 = 1000; ~7 = 1000 8&~ 7 = 1000&1000 = 1000; So even if it's 1 it goes up to 8, so it's 8 bytes aligned.Copy the code
  • 6. Determine the size of the following data structures
union kc_t { unitptr_t bits; struct { int a; char b; }}Copy the code
  • 8 A,
  • B, 12
  • 13 C,
  • D, 16
A kc_t is A union, unitptr_t is 8 bytes, struct is 8 bytes, but it does not add, because the data inside the union is A mutually exclusive relationship.Copy the code
  • 7, metaclass isa to who, root metaclass superclass is who

  • A. self, root metaclass

  • B, self, NSObject

  • C, root metaclass, root metaclass

  • D, root metaclass, NSObject

B) the bitmap of isaCopy the code

  • 8, the method cache is found to be out of order, why? How are hash conflicts resolved
  • A, : hash function cause, not solved
  • B, : hash function reason, again hash
  • C, : He deposited his I also phuket island, again hash
  • D, : He chaos by his chaos, the wind over the hills, not to solve
B
Copy the code
  • 9. The flow of messages is
  • A, : quick search from the cache first
  • B, : Slowly recursively lookup methodList (own and parent until parent is nil)
  • C, : dynamic method resolution
  • D, : message forwarding process
A B C D
Copy the code
  • Why implement resolveInstanceMethod later

  • A, : Class methods have metaclasses (in the form of object methods) whose superclass is ultimately NSObject so we can prevent object methods from being implemented in NSObject with resolveInstanceMethod!

  • B, : because at the bottom of oc, object methods ultimately exist

  • C, : Class method exists Metaclasses exist in the form of object methods.

  • D, : salty eat radish, light worry! Don’t worry about what Apple wrote

A
Copy the code

True or false

  • Based on our object address, we can not confirm whether the object has an associated object — ❌
The second 'has_asSOC' : associated object flag bit in the ISA underlying structure, 0 does not exist, 1 existsCopy the code
  • 12, int c[4] = {1,2,3,4}; int *d = c; C [2] = *(d+2) — ✅
  • @interface LGPerson: NSObject{UIButton * BTN} @interface LGPerson: NSObject{UIButton * BTN
  • 14. Superclass of metaclass except NSObject = metaclass of superclass — ✅
  • 15. The address of the object is the first address of the memory element — ✅
  • 16. Classes are objects — ✅

Short-answer questions

  • 17, how to restore the upper OC code to C++ code (2 ways)
2, xcrun - SDK iphoneos clang-arch arm64 - rewriteobjc xx.m-o XXXX. CPPCopy the code
  • How to open the assembly to view the process, what are the benefits?
Xcode-debug-debug Workflow-always show Disassembly Is a simple way to see how functions are executed at the assembly level. It provides information for objC source analysis to avoid directional errors. Combined with memory reads, you can see more clearly how registers work with each other. Using the assembly view process, you can track down the internal code and find the source without being sure of the source and the execution process! At the same time, the combination of symbolic breakpoints, can be more clear with the source implementation.Copy the code
  • X /4gx and p/x and p*$0
X /4gx: output a memory address, in the form of 8 bytes output 4 end P /x: output the first address of a data structure P *$0: $0 is a pointer to a data space, and the instruction output is the data structure of the dataCopy the code
  • Where do class methods exist? Why do we do this?
Object methods are stored in classes, and class methods are stored in metaclasses; Object methods are instantiated by classes, and classes are instantiated by metaclasses; This design is made from the following three aspects: 1. The bottom layer does not need to distinguish between class methods and object methods. In essence, they are all object methods. In this way, the design is more object-based. All the information of the class is stored in the metaclass, and all the information of the object is stored in the Class. The class is the instantiated object of the metaclass, and the object is the instantiated object of the class. OC language design borrowed early on from another Smalltalk language, samllTalk.Copy the code
  • Method of binary search process in the slow search process, please use pseudo code.
The premise of binary search is that the data must be arranged in order; 2. Constantly find the starting position (base) and the amount of valid data (count); 3, current target location = start location (base) + valid data volume (count) / 2; 4. After looking for the corresponding position, it keeps moving forward, mainly in order to find the first data that meets the condition, that is, to find the method that meets the condition in the first category.Copy the code
  • ISA_MASK = 0x00007FFFFFFff8ULL what is the meaning of this ISA_MASK algorithm?
This algorithm is mainly for obtaining the class information stored in isa. Most isa are impeccable isa, which isa combination of bit-domain data advocating 64gebyte. The part that stores class information only has part of the byte location, and the rest of the position stores other information. Isa_maske is used for bitwise and bitwise operations, and only is_maske byte data is preserved. The purpose of this algorithm is to remove the useless data. This will allow isa to store more information.Copy the code
  • Why are there rW, ro and RWE in the class structure?
Ro is read-only and belongs to clean memory. The space of memory is determined at compile time and does not change the space of content after loading. Rw is a run-time structure in which properties, methods, etc., can be added to a class, which changes at run time. Rwe is the separation of the parts of RW that are not commonly used in order to save memory. Because very few classes actually change their methods. At runtime, if you need to dynamically add methods, protocols, etc., to a class, you will create an RWE and attache the ro data to the RWE first. When reading, you will return rWE data first. If the RWE is not initialized, you will return RO data. Rw includes ro and rwe, where rw is dirtymemory and ro is clean memory. To make the dirty memory take up less space, the variable parts of the RW are extracted as RWE; The more clean memory, the better, and the less dirty memory, the better. Because of the underlying virtual memory mechanism of iOS, when the memory is insufficient, part of the memory will be reclaimed. When the memory needs to be used again, it will be loaded from the hard disk, that is, swap,clean memory Is memory that can be reloaded from the hard disk. The Macho file dynamic library in iOS belongs to this type. The dirty memory is the data generated during the running. The data cannot be reloaded from the hard disk, so it must always occupy the memory. When the physical memory of the system is tight, the clean memory memory is reclaimed. So the more clean memory the better, the less dirty memory the better; Apple makes such a detailed division of rw, ro and rwe in order to better and more detailed distinction between cleanmemory and dirty memory;Copy the code
  • When is cache capacity expanded and why?
1. In general, if the capacity of the cache exceeds 3/4 of the total capacity after the current method is used, then the expansion operation is performed, doubling the capacity of the previous method, and then the current method is inserted. 2. Under some special preprocessing macro definition compilation commands, the storage will be full for the first time before expansion; 3. The selection of 3/4 as the load factor during capacity expansion is related to the linked list used at the bottom of the hash table and the data structure of the red-black tree. 0.75 is the value most consistent with the probability of The Posson distribution, and the hash table has the highest spatial and time efficiency under this value.Copy the code
  • Why is objc_msgSend written in an assembly? How does objc_msgSend recursively find imp?
1, the use of assembly response speed; 2. Two cycles are used in this process; Loop 1: Through the previously obtained mask, hash with the _cmd to be searched, obtain the subscript, so as to get the bucket address corresponding to _cmd, and then move forward the search, 16 bytes each time, if the corresponding SEL is found, then caheHit; When the first address is not found, the second loop is entered. Loop 2: first, get the last bucket to the address, and also use the forward search method, to _cmd corresponding address for translation search.Copy the code
  • 26. Why can a class call an object method of the same name on NSObject when its class method has no implementation?
This is designed to work with isa and superclass routing and method lookup logic. First, the isa of the class points to the metaclass. In a fast method search, the metaclass will be found according to the ISA of the class. If there is no method change in the metaclass, it will go to a slow lookup process of lookUpImpOrForward. In the slow lookup process, we do a for recursive lookup, and we find the metaclass's parent class, the root class, in terms of superclass, and the superclass of the root metaclass points to class NSObject, so we call an object method of the same name on NSObject.Copy the code