iOS Basics-instance variables four types of scopes

Source: Internet
Author: User

1. 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, Apple also lets developers display the set range (four choices).

2. The four compiler directives are as follows:

@private

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

@protected

An instance variable can be declared with its class and subclass, and all instance variables that do not display the specified range are @protected.

@public

Instance variables can be accessed from anywhere.

@packge

With the modern runtime, a @package instance variable is actually @public in the implementation of this class's executable image, but outside is @private.

The @package in OC is similar to the private_extern of variables and functions in C language. Any use of this instance variable outside the mirroring of the implementation class will cause link error to be thrown.

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

iOS Basics-instance variables four types of scopes

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.