The range type of four instance variables in OC @private@protected@public@package

Source: Internet
Author: User

To enforce the ability of a object to hide it data, the compiler limits the scope of instance variables-that is, limits Their visibility within the program.

To force an object to hide its data, the compiler restricts the scope of the instance variable to limit its visibility in the program

But to provide flexibility, it also lets you explicitly set the scope at four levels. Marked by a compiler directive:

But to provide flexibility, Apple also allows developers to explicitly set the range (four choices)

Directive

Meaning

@private

The instance variable is accessible only within the class, that declares it.

@protected

An instance variable can be accessed only by the class that declares it

The instance variable is accessible within the class, the declares it and within classes that inherit it. All instance variables without an explicit scope directive has @protected scope.

An instance variable can be declared to be accessed by its classes and subclasses, and all instance variables that do not explicitly define the scope are @protected

@public

The instance variable is accessible everywhere.

Instance variables can be accessed from anywhere.

@package

Using the modern runtime, an @package instance variable have @public scope inside the executable image that implements the class, but acts like @private outside. When using the modern runtime, a @package instance variable is actually @public in the implementation of this class's executable image, but outside is @private " Runtime need to look at the Apple Document Runtime Programming Guide

The @package scope for OBJECTIVE-C instance variables are analogous to Private_extern for C variables and functions. Any code outside the class implementation ' s image, tries to use the instance variable gets a link error.

The @package in Objective-c is similar to the private_extern of variables and functions in the C language. Any code that is outside the mirroring of the implementation class that wants to use this instance variable will cause link error

This is the most useful-instance variables in the framework classes, where @private may was too restrictive but @protecte D or @public too permissive.

This type is most commonly used for instance variables of framework classes, using @private too restrictive, using @protected or @public and too open

The range type of four instance variables in OC @[email  Protected] @[email  Protected]

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.