This is the 8th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021.

In Auto Layout, we used the keyword Fileprivate. Today we will introduce it.

Access control

The concept of access control enables us to restrict how other code accesses types, functions, and other declarations. Swift provides five different levels of access control, and taking advantage of them is critical to writing programs with a clear separation of concerns and a robust structure. First, put the official address on the list as usual: access control

There are five different levels of access control that relate not only to the entities defined in the source file, but also to the module to which the source file belongs. Here are the five different levels.

internal

When we define any new type, attribute, or function in Swift, it has internal access by default. This means that it will be visible to all other code that resides in the same module — such as App, Extension, Framework, or Swift Package.

internal class Test { func test() { // internal func test //TODO:... }}Copy the code

public

Because the code above does not currently specify any explicit access level, we can access the testInternal method in the Test class from anywhere in the app. But if we want to share our new class with other modules (for example, we might implement it in a framework shared between our main application and an extension or a companion Apple Watch application), then we’ll need to use public to access the class outside of these:

public class Test { public func test() { //TODO:... }}Copy the code

We are now able to find, initialize, and call our module both inside and outside of its module. But now suppose we also want to subclass it to modify it or add new functionality to it. Using public while this is possible in its own module, these operations are not allowed outside of its own module. At this point you will need to use the keyword open:

open

open class Test { open func test() { //TODO:... }}Copy the code

With open, we can create custom Test subclasses anywhere — they can have new initializers, new properties, and new methods.

open class SubTest : Test { func test() { //TODO:... }}Copy the code

As mentioned above, the access control level goes from default to expanded scope. We want to keep the scope in the same file, so we need to use Fileprivate.

fileprivate

class Test { fileprivate var num : Int = 0 fileprivate func testPublic() { //TODO:... }}Copy the code

Here I can only access it within the same file class or extension:

class TestPublic {
    func testPublic() {
       //TODO:...
        let test = Test()
        test.num = 1
    }
}
Copy the code

At this point we think access control is narrowing. You want and limit the use of an entity to the entity declaration and its extensions, which must be defined in the same file. This is where the keyword private comes in.

private

class Test {
    private var num : Int = 0
}
Copy the code

So we can default to zoom in and default to zoom out too easily.

The open and public

Allows entities to be used outside of defining modules (targets), typically using open or public access when specifying the framework’s public interface.

  • open: This access level is ubiquitous, accessible within functions, classes, files, outside targets, and even overrides
  • publicAnd:openSame, but cannot override.

However, open access applies only to entities and entity members, and differs from public access as follows:

  • Public entities and entity members can only be subclassed and overridden within the definition module (target).
  • Open entities and entity members can be subclassed and overridden both inside and outside the defining module (target).
Framework_wift open class A {} // framework_wift public class B: A {} // ok // Second. Framework -- c.wift import First internal class C: Framework -- d.swift import First internal class D: B {} // error: B cannot be subclassedCopy the code

Fileprivate and private

  • fileprivate: If multiple classes exist in the same file, this access level area is within the file and also accessible. Restrict the use of entities to the files they define.
  • private: When you write private member access, but the property “private member” can only be used within the scope of the entity to include the same file extension.
Fileprivate: // first. framework -- a.wift internal struct A {fileprivate static let x: Int} // first.framework -- a.swift A.x // ok // first.framework -- b.swift A.x // error: X is not available private: // first.framework -- a.wift internal struct A {internal static func test() {x // ok}  internal class C { internal static func test() { A.x // error: x is unavailable } }Copy the code

Pay attention to the point

The open, public, internal, Fileprivate, and private modifiers cannot be used in the protocol.

conclusion

So, to sum up, these are the five levels of access control that Swift currently provides:

  • privatePreserves an attribute or function in its enclosing type, including any extensions of that type defined in the same file. When applied to top-level types, functions, or extensions, it has the same effect asfileprivate.
  • fileprivateMake the declaration visible throughout the file in which it is defined, while hiding it from all other code.
  • internalIs the default access level and makes the declaration visible throughout the module that defines it.
  • publicFunctions, types, extensions, or properties can be accessed outside of their modules.
  • openAllows classes to subclass outside of their modules and override functions or attributes.