Class:
@interface ClassA : NSObject{
}
@property(nonatomic, retain) Foo* foo;
@end
Class B:
@interface ClassB : NSObject{
Foo* foo;
}
@property(nonatomic, retain) Foo* foo;
@end
The difference between the two classes is that the second class contains an ivar named "foo.
If the implementation part of the class contains "@ synthesize foo;" and a newer compiler is used (as if starting from 4.2), for the first class, the compiler automatically adds an appropriate ivar for the class,
You can use foo directly, just like defining the member variable foo.
The synthesis mechanism of ivar relies on Objective-C's "fixing vulnerable base class features ".
In most compiled languages, ivar accesses are implemented through the "object address + ivar offset" method,
The ivar in the subclass must be behind the base class ivar so that the subclass must know the size of the base class object to calculate the start address of its own ivar.
When you change the base class size, such as increasing or decreasing ivar, you must re-compile all subclasses.
Objective-C solves the "vulnerable base class" by using the size of all ivar as a metadata, storing it in the metadata of the class, and setting it at runtime.
In this way, you can use the "object address + base class size + ivar offset" method to calculate the corresponding ivar address and access the corresponding ivar.
The underlying mechanism of "ivar synthesis" depends on the compiler. In short, the compiler adjusts the ivar region to dynamically add ivar.