Swift description of open, public, fileprivate, private, internal, modifier, swiftfileprivate

Source: Internet
Author: User

Swift description of open, public, fileprivate, private, internal, modifier, swiftfileprivate

The differences between open, public, fileprivate, private, and internal in swift are as follows:

Open, public, fileprivate, private, and internal modifiers are used to modify the access level.

The level corresponding to open and public is that this module or the module that references this module can be accessed, that is, a belong to A and B import A, both of which can be accessed by.

Internal can be accessed inside the module, but cannot be accessed outside the module. a belong A, B import A, A can access a, B cannot access.

Fileprivate is similar to the name. file private is the private relationship between files, that is, it can still be accessed in the same source file, however, you cannot access a belong to file A, a not belong to file B in other files. You can access A in file a, but not a in file B.

And private are more restrictive than fileprivate. private acts on A class. That is to say, for class a, if attribute A is private, it cannot be accessed in other places except class.

 

First, you can see the differences at the end of the article in English or links.

Https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3

Swift provides five differentAccess levelsFor entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs.

  • Open accessAndPublic accessEnable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. you typically use open or public access when specifying the public interface to a framework. the difference between open and public access is described below.

  • Internal accessEnables entities to be used within any source file from their defining module, but not in any source file outside of that module. you typically use internal access when defining an app's or a framework's internal structure.

  • File-private accessRestricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

  • Private accessRestricts the use of an entity to the enclosing declaration. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.

Open access applies only to classes and class members, and it differs from public access as follows:

  • Classes with public access, or any more restrictive access level, can be subclassed only within the module where they're defined.

  • Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they're defined.

  • Open classes can be subclassed within the module where they're defined, and within any module that imports the module where they're defined.

  • Open class members can be overridden by subclasses within the module where they're defined, and within any module that imports the module where they're defined.

Marking a class as open explicitly indicates that you 've considered the impact of code from other modules using that class as a superclass, and that you 've designed your class's code accordingly.

Guiding Principle of Access Levels

Access levels in Swift follow an overall guiding principle:No entity can be defined in terms of another entity that has a lower (more restrictive) access level.

For example:

  • A public variable cannot be defined as having an internal, file-private, or private type, because the type might not be available everywhere that the public variable is used.

  • A function cannot have a higher access level than its parameter types and return type, because the function cocould be used in situations where its constituent types are not available to the surrounding code.

The specific implications of this guiding principle for different aspects of the language are covered in detail below.

 

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.