A description of Objective-C iVar

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.