This chapter content

  1. WWDC2020 three optimizations for Runtime
  2. How the class is loaded (lazy loading)
  3. Instance variables, attributes, and member variables
  4. Copy differs from where the other modifiers are
  5. How is the objc_setProperty method called underneath
  6. Method encoding
  7. Runtime methods

How the class is loaded

This time, I will simply verify that the loading mode of the class is lazy, and I will not go into the details of why and how. For example, the inheritance relationship is Teacher -> Person -> NSObject.

Note that our validation process is based on the objC source code project, and that when out of the source code environment we need to sample ourselves, i.e. copy the source code into the project

Process:

Address of the Person class pointer —-> Shift 0x20 to get bits —-> Call class_rw_t* data() to get class_rw_t* —-> Output the class_rw_t structure to view —–> Call the teacher.class method —-> Output the class_rw_t structure to view

Validation:

Person *p = [Person alloc] Person *p = [Person alloc] Person *p = [Person alloc] Person *p = [Person alloc] Using bits we can get a pointer to the class_rw_t structure. If in doubt, look at the nature of the class and see that firstSubclass is empty.

Addition: nextSiblingClass indicates that the class next to the Person class in memory is NSUUID

Person class_rw_t (‘ firstSubclass ‘, ‘Teacher’)

Instance variables, properties, member variables

1, property: ivar + setter + getter, that is, member variables + the system automatically generated for the property setter and getter methods

2, member variables: only ivar, such as int, char, double, float, string, bool, etc. The underlying compilation is for regular data types

3. Instance variables: Special member variables, such as NSObject, are considered constructs in the underlying compilation

Validation: Verify with clang

For example: the Person class we created

Look at it after you convert it to a CPP file

Special to the copy modifier

When we talk about copy, we refer to it as copy. If the object is to implement the NSMutableCopying protocol for a deep copy, and if the object is to implement the NSCopying protocol for a shallow copy, then the deep copy copies the content, and the shallow copy copies the pointer, i.e. the pointer reference. Depth copy is attached

1. Look at the Person class

Five different attributes are defined

2. View the C++ source code converted by clang

1. Look at the structure of the object Person

2. Look at the setter and getter for the property nonatomic_strong_name and get strong.

3, look at attributes nonatomic_copy_name setter and getter methods, get the copy is property of modified getter method is isa + translation memory, the setter is calledobjc_setPropertymethods

4, see attribute nonatomic_name setter and getter methods, like strong is isa + translation memory

Atomic_copy_name atomic_copy_name atomic_copy_name atomic_copy_name atomic_copy_name atomic_copy_name atomic_copy_name atomic_copy_name atomic_copy_name atomic_copy_name

Look at the setter and getter for atomic_name, which is isa+ memory translation as strong

3, questions and summary

Question: Why is nonatomic_copy_name different from atomic_copy_name, why is objc_setProperty called, Why do we call objc_getProperty when atomic and copy are decorated together

conclusionObjc_setProperty is called if the setter method is modified by copy, and objc_getProperty is called if the setter method is modified by copy. This is because Apple builds the framework LLVM layer based on these two attributes. Only the LLVM results are shown here

Objc_setProperty (objC

It’s not just objc_setProperty it’s objc_setProperty_atomic, objc_setProperty_atomic, objc_setProperty_nonatomic, objc_setProperty_atomi The c_copy and objc_setProperty_nonatomic_copy methods all point to the final method —–> reallySetProperty.

Pay attention toThe underlying atomicity is just a lock, for read security, but read security does not mean thread safety. One question that is often asked in an interview is atomic really thread-safe? The answer is not necessarily: for example, asynchronous thread A does the assignment and thread B does the read, and each of these operations loops many times.

Objc_getProperty objC source code

Method encoding

When looking at the output of a method in LLDB, we often see some encoding such as:

  1. V stands for return type void
  2. 16 represents the size of the total required bytes for the method parameters. 16 bytes
  3. At sign means that the first argument is of type ID
  4. 0 means that the first argument starts with byte bit 0
  5. : represents the second parameter type SEL
  6. 8 means that the second argument starts with byte bit 8

Summary: Each character represents a different encoding. Check the code reference table.

Runtime methods

The following is copied from my own notes, so make do with it