简体   繁体   中英

Objective-C ivar visibility 'package'

When creating ivars in Objective-C the default visibility is 'protected', meaning the ivar can be accessed from subclasses. (If it is declared in the header).

Therefore this code:

@interface MagicCarpet : NSObject
{

@protected
    NSString* _threadCount;
}

is the same as this:

@interface MagicCarpet : NSObject
{
    NSString* _threadCount;
}

Apparently, there's also a 'package' level visibility. I frequently see and use 'public', 'private' and 'protected' visibilities. . . have never seen any code with 'package'. What does it do?

From the Apple docs:

@package is a new instance variable protection class, like @public and @protected . @package instance variables behave as follows:

  • @public in 32-bit;
  • @public in 64-bit, inside the framework that defined the class;
  • @private in 64-bit, outside the framework that defined the class.

    In 64-bit, the instance variable symbol for an @package ivar is not exported, so any attempt to use the ivar from outside the framework that defined the class will fail with a link error.

This logic seems quite confusing, so I'm not at all surprised that @package is not commonly used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM