swift 中關於open ,public ,fileprivate,private ,internal,修飾的說明


關於 swift 中的open ,public ,fileprivate,private, internal的區別 以下按照修飾關鍵字的訪問約束范圍 從約束的限定范圍大到小的排序進行說明

open,public,fileprivate,private,internal 這幾個修飾詞的作用是用於修飾訪問級別的。

、open,public 對應的級別是該模塊或者是引用了該模塊的模塊可以訪問 即 a belong to A , B import A 這兩種情況都可以對 a進行訪問

public: 類用public(或級別更加等級更低的約束(如private等))修飾后只能在本模塊(sdk)中被繼承,如果public是修飾屬性的話也是只能夠被這個module(sdk)中的子類重寫

open:用open修飾的類可以在本某塊(sdk),或者其他引入本模塊的(sdk,module)繼承,如果是修飾屬性的話可以被此模塊或引入了此某塊(sdk)的模塊(sdk)所重寫

、internal 是在模塊內部可以訪問,在模塊外部不可以訪問,a belong A , B import A, A 可以訪問 a, B 不可以訪問a.比如你寫了一個sdk。那么這個sdk中有些東西你是不希望外界去訪問他,這時候你就需要internal這個關鍵字(我在導入第三方框架時發現其實沒有定義的話sdk里面是默認internal的)

、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外其他地方都不能訪問了(fileprivate 和private都是一種對某個類的限制性約束。fileprivate的適用場景可以是某個文件下的extension,如果你的類中的變量定義成了private那么這個變量在你這個類在這個類的文件的拓展中就無法訪問了,這時就需要定義為fileprivate)

 最后是 Guiding Principle of Access Levels (訪問級別的推導原則):不能在低級別的修飾中定義比自身更高的級別修飾,如public不能修飾在private類中的屬性

首先說明一下這里面的區別在文章結尾英文或鏈接中可以看得到

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:(public 和 open 的區別)

  • 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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM