1. The nature of the object

In the last blog iOS development of the structure of the underlying exploration has been said, the nature of the object is a structure, so there may be some friends do not understand. So let’s review it and explore it briefly with our friends. I just need to create a project, create a Student class, use Clang command, compile into the underlying source code, and then look inside the source code to know.

@interface Student : NSObject
@end

@implementation Student
@end
int main(int argc, const char * argv[]) {
	@autoreleasepool {
		Student *stu = [[Student alloc]init];
	}
	return 0;
}

Copy the code

We can see from the following 👇 compiled source code, it is obvious that the underlying structure of the object is a structure.

You’re right? What a coincidence! Doesn’t mean anything, does it?

Then go on to 👇

@interface Student : NSObject

@property (nonatomic.readwrite , copy) NSString *StudentName;

@end

@implementation Student
@end
Copy the code

Now give the Student a StudentName attribute, take a look at the output of the source code. The StudentName attribute is clearly visible in the underlying source code, which confirms that the object is a structure. The Student_IMPL structure is the actual structural representation of the Student object’s essence in the underlying source code.

You can also see the setter and getter methods for StudentName.

Typepedef struct objc_object Student (typepedef struct objc_object Student) Because on top the OC representation is of type NSObject, and on bottom objc_Object is the underlying representation of NSObject, and Student inherits from NSObject.

NSObject Underlying source code structure

#ifndef _REWRITER_typedef_NSObject
#define _REWRITER_typedef_NSObject
typedef struct objc_object NSObject;
typedef struct {} _objc_exc_NSObject;
#endif

struct NSObject_IMPL {
	Class isa;
};

Copy the code

Many partners also want to play with the underlying source code, so I detailed the commonly used command introduction, convenient partners to explore their own learning.

Clang is a lightweight compiler for C, C++, and Objective-C. Source code is published under the BSD protocol.

Clang is an Apple-led, LLVM-based C/C++/Objective-C compiler. In April 2013,Clang fully supported the C++11 standard and began implementing C++ 1Y features (i.e. C++14, the next minor update to C++). Clang will support its normal lambda expressions, simplified handling of return types, and better handling of constEXPr keywords. Objective-c ++ compiler. It is almost completely compatible with the GNU C language specification (although there are some incompatibables, including some differences in compiler command options) and adds additional syntactical features such as C function overloading (which modifies functions by __attribute__((overloadable)), One of its goals is to go beyond GCC.

Clang-rewrite-objc main.m -o main. CPP compiles the object file into a c++ file

Xcrun is installed with Xcode. Xcrun encapsulates clang to make it easier to use.

The emulator uses the following command

xcrun -sdk iphonesimulator clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpp

The real machine uses the following command

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o mainarm64.cpp

2.isa

The Student underlying structure is 👇 below

struct Student_IMPL {
	struct NSObject_IMPL NSObject_IVARS;
	NSString *_StudentName;
};
Copy the code

But the underlying NSObject looks like this

struct NSObject_IMPL {
	Class isa;
};
Copy the code

As you can see from the Student_IMPL structure, there is a member of NSObject_IVARS which is also a structure and of type NSObject_IMPL. Struct NSObject_IMPL NSObject_IVARS (struct NSObject_IMPL NSObject_IVARS)

struct Student_IMPL {
	Class isa;
	NSString *_StudentName;
};
Copy the code

This isa looks particularly familiar. In my alloc low-level exploration blog for iOS development, we talk about class associations that return the hexadecimal address of an object after calloc is used to open up memory. However, we found that this is not the same as the

print format that we normally print. Isa does not associate our object with our class.

As you can see from the above code, isa is of Class type. What is Class? I can find the answer from the underlying source code, as follows 👇

typedef struct objc_class *Class;

struct objc_object {
    Class _Nonnull isa __attribute__((deprecated));
};
typedef struct objc_object *id;

typedef struct objc_selector *SEL;
Copy the code

Class is the alias of the objc_class structure. You can also see that there is an ID inside it, which is the id we usually use

Due to the length of this article, the content of the end

For more, see initIsa, the nature of objects and associative features of classes explored at the bottom of iOS.

🌹 please bookmark + follow, comment + forward, in case you can’t find me next time, ha ha 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, learn from each other 😁, improve themselves 🌹