We all know that an object is essentially a structure, so I’m going to explore that today
First, object low-level compilation
Let’s start with some code:
// Write directly in main.m (@interface AAPerson: NSObject @end @implementation AAPerson @end int main(int argc, const char * argv[]) { @autoreleasepool { AAPerson *per = [AAPerson alloc]; } return 0; }Copy the code
Let’s compile it to a c++ file using clang and see what the underlying compilation looks like:
// Go to the folder and run the clang command clang-rewrite-objc main.m -o main.cppCopy the code
Look at the compiled code:
#ifndef _REWRITER_typedef_AAPerson
#define _REWRITER_typedef_AAPerson
typedef struct objc_object AAPerson;
typedef struct {} _objc_exc_AAPerson;
#endif
struct AAPerson_IMPL {
struct NSObject_IMPL NSObject_IVARS;
};
/* @end */
// @implementation AAPerson
// @end
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
AAPerson *per = ((AAPerson *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("AAPerson"), sel_registerName("alloc"));
}
return 0;
}
Copy the code
typedef struct objc_object AAPerson; AAPerson really is a structure at the bottom, objC_Object.
struct NSObject_IMPL NSObject_IVARS; The structure contains an NSObject_IVARS, derived from the NSObject_IMPL
NSObject_IMPL is also a structure that contains an ISA
struct NSObject_IMPL {
Class isa;
};
Copy the code
Conclusion:
The essence of an object isa structure that inherits a property from its parent, an isa pointer.
Second, object add attribute bottom compilation
@interface AAPerson : NSObject @property (nonatomic, copy) NSString *name; Attribute @end @implementation AAPerson @endCopy the code
The compiled:
extern "C" unsigned long OBJC_IVAR_$_AAPerson$_name;
struct AAPerson_IMPL {
struct NSObject_IMPL NSObject_IVARS;
NSString *_name;
};
Copy the code
After compiling, it helps us to produce an underlining _name and getter method:
// Getter method // The underlying compiler passes in two arguments by default: Id the self, SEL _cmd static NSString * _I_AAPerson_name(AAPerson * self, SEL _cmd) { return (*(NSString **)((char *)self + OBJC_IVAR_$_AAPerson$_name)); }Copy the code
There is also a setter method:
extern "C" __declspec(dllimport) void objc_setProperty (id, SEL, long, id, bool, bool); Static void _I_AAPerson_setName_(AAPerson * self, SEL _cmd, NSString *name) {objc_setProperty (self, _cmd, NSString *name) __OFFSETOFIVAR__(struct AAPerson, _name), (id)name, 0, 1); }Copy the code
Also, inside the method_list structure:
"@16@0:8" indicates the type of the parameter. @ Return value type. Id type 16 indicates the length of the entire parameter. Parameter 2 type, SEL type 8 means start from 8, Account for 8 ~ 15 * / {{(struct objc_selector *) "name", "@ @ 0:8 16," (void *) _I_AAPerson_name}, Struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_AAPerson_setName_},Copy the code
There is an official reference list:
Conclusion:
Object property, and the system will help us produce _name,getter,setter at compile time.
Object add member variables underlying compilation
@interface AAPerson : NSObject{
NSString *sex;
}
@end
@implementation AAPerson
@end
Copy the code
The compiled:
struct AAPerson_IMPL {
struct NSObject_IMPL NSObject_IVARS;
NSString *sex;
};
Copy the code
When compiled, it didn’t help us produce a _sex to be underlined, and there were no getters and no setters
One more detail: declare inside {} that a class that produces instance objects will have a nickname: instance variable.
@interface AAPerson : NSObject{ NSString *name; // Member variables: the underlying compilation does not generate the corresponding setter getter AATeacher *t; // Instance variable: is a special type of member variable (instanceid (void *))}Copy the code
Conclusion:
Object, does not generate _ properties, getters, setters
Four,
- The essence of an object is a structure
- Object properties, which automatically generate _ properties, getters, setters underneath
- Object, does not generate _ properties, getters, setters