1. Objective-c code, the underlying implementation is actually C++ code. The conversion process is as follows:

2. Objective-c objects and classes are mainly implemented based on C\C++ which data structure?

The structure of the body

3. Convert Objective-C code to C\C++ code:

Xcrun - SDK iphoneOS clang -arch arm64 -rewrite-objc OC source file -o xxx.cpp

  • Convert main.m to C++ code:
  • The business code is at the bottom of the generated main.cpp:

4. How is an OC object laid out in memory?

  1. First look at the implementation of NSObject in Xcode as follows:

    • After removing the macros, it looks like this:
    @interface NSObject {
        Class isa;
    }
    Copy the code
  2. You can then find the NSObject_IMPL structure in the generated source code main.cpp

    • NSObject_IMPL basically means NSObject Implementation
  3. So we can understand that NSObject is finally converted to a C++ structure:

  4. The memory allocation for creating an NSObject object looks like this: