swift 中關於open ,public ,fileprivate,private ,internal,修飾的說明,swiftfileprivate
關於 swift 中的open ,public ,fileprivate,private, internal的區別 以下按照修飾關鍵字的訪問約束範圍 從約束的限定範圍大到小的排序進行說明
open,public,fileprivate,private,internal 這幾個修飾詞的作用是用於修飾存取層級的。
、open,public 對應的層級是該模組或者是引用了該模組的模組可以訪問 即 a belong to A , B import A 這兩種情況都可以對 a進行訪問
、internal 是在模組內部可以訪問,在模組外部不可以訪問,a belong A , B import A, A 可以訪問 a, B 不可以訪問a.
、fileprivate 這個修飾跟名字的含義很像,file private 就是檔案之間是private的關係,也就是在同一個source檔案中還是可以訪問的,但是在其他檔案中就不可以訪問了 a belong to file A, a not belong to file B , 在 file A 中 可以訪問 a,在 file B不可以訪問a
、private 這個修飾約束性比fileprivate的約束性更大,private 作用於某個類,也就是說,對於 class A ,如果屬性a是private的,那麼除了A外其他地方都不能訪問了
首先說明一下這裡面的區別在文章結尾英文或連結中可以看得到
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3
Swift provides five different access levels for 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 to.
Open access and public access enable 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 access enables 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 access restricts 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 access restricts 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 could 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.