【iOS Notes 】iOS notes series directory

Swift has five levels of access control, such as Open, Public, Internal, Fileprivate, and private, in descending order.



The basic principles they follow are:

High-level variables are not allowed to be defined as members of low-level variables. For example, a private class cannot contain a public String. On the other hand, low-level variables can be defined in higher-level variables, such as public classes that contain private ints.

  • Open has the highest access rights. The modified classes and methods can be accessed and inherited from any Module; It is a new access right added to Swift 3.

  • Public is second only to Open in permissions. The only difference from Open is that it modifies objects that can be accessed in any Module, but can only be inherited in the current Module.

  • Internal is the default. It can be accessed and inherited only in the currently defined Module. It can be accessed by multiple files in a Module, but not by other modules.

  • Fileprivate is also a new permission for Swift 3. The object it decorates can only be used in the current file. For example, it can be used by class, extension, and struct in one file.

  • Private is the lowest access. Its objects can only be used within the defined scope. Outside of this scope, even other scopes in the same file are not accessible, but are accessible in Extension.