Access control details for the Swift tutorial

  • 2020-06-01 11:07:18
  • OfStack

preface

This article introduces you to the Swift access control, which restricts access to other source files and module parts of your code. This feature allows you to hide your code implementation and specify the preferred interface through which the code can be accessed and used.

class, structure, and enumeration can all specify access levels, of course, property, method, initializer, and subscript of the type here. protocol can be restricted to a context, as can global variables, variables, and functions.

In addition, Swift also provides a default usage level for typical usage scenarios. Indeed, if you were to write an app for a single 1 target, you might not need to specify the level of access control explicitly at all.

Modules and source files

Swift's access control model is based on the concept of modules and source files.

A module is a single code distribution unit -- an framework or application is compiled and delivered as a single unit that can be imported by other modules via Swift's import keyword.

In Swift, each build target (e.g., 1 app bundle or framework) of Xcode is treated as a separate module.

Although it is common practice to define different types in different source files, a single source file can actually contain different definitions for types, functions, and so on.

The level of access

Swift provides five different levels of access to your code entities:

Open access and public access allow entities to be used in any source file from a determining module, or from another imported module. Typically, open or public are used to specify the framework public interface. The differences between the two are described below. Internal access allows entities to be used in any source file where they define the model, but not in any source file outside the module. internal is usually used when defining the internal structure of an app or an framework. Access to File-private limits the use of entities in the defined source files. Use the file-private access to hide the implementation details of specific functions when they are used throughout the file. The Private access limits the use of the entity to a closed declaration. Use private access to hide the implementation details of specific functions when they are used in a single declaration.

Open access is the highest access level and private is the lowest access level (maximum restriction).

The Open access only applies to classes and class members. The difference between publick access and publick access is as follows:

Classes accessed using public, or other more restrictive access levels, can only be subclassed within a defined module. Class members accessed using public, or other more restrictive access levels, can only be subclassed within a defined module. The Open class can be subclassed by defined modules or other import modules. Members of the Open class can be overridden by defined modules or by subclasses created elsewhere in the import module.

In simple terms, the difference between public and open is that public has less class inheritance outside the module and class member override rights than open.

Guidelines for access levels

In Swift, the access level follows the general guideline that no entity can be defined within another entity with a lower access level (more restricted).

Such as:

The public variable cannot be defined as having an internal, file-private, or private type, because this type may not be used anywhere where public variables are used. A function cannot have a higher level of access than other parameter types and return types, because the function can be used when its constituent type is not available to surrounding code.

More on that later.

The default access level

If you don't specify a specific access level yourself, all entities in your code have a default internal access level. As a result, in many cases you don't need to specify a specific level of access to your code.

The access level of a single target application

If you write a simple single target application for i, then your application code is typically self-contained and does not need to be used outside the application module. The default access level, internal, already meets this requirement. Therefore, you do not need to specify an access level. However, you may need to mark parts of your code as file private or private so that other code in the program module hides their implementation details.

Access level for Frameworks

When you develop an framework, mark open or public so that it can be accessed by other modules, such as a program that introduces this framework. The public-facing interface is the programming interface of framework (or API).

Note that any internal implementation details of framework can also use the default internal access level, or can be identified as private or file private, if you want to hide them from the rest of the internal code of framework. Only if you want an entity to be part 1 of your framework API will you need to identify the entity as open or public.

Unit tests the access level of the target

When you write a program that contains unit test objectives, you need to make the code in your program usable by the test module for easy testing. 1 normally, only entities identified as open or public can be accessed by other modules. However, if you add the @testable attribute to the product module import before declaring it and compile the product module under the open test option, the unit test target will be able to access any internal entity.

Access-level syntax

Define the access level for the entity:


public class SomePublicClass {}
internal class SomeInternalClass {}
fileprivate class SomeFilePrivateClass {}
private class SomePrivateClass {}
 
public var somePublicVariable = 0
internal let someInternalConstant = 0
fileprivate func someFilePrivateFunction() {}
private func somePrivateFunction() {}

Unless otherwise specified, the default access is internal, which means that SomeInternalClass and someInternalConstant can have access to internal without specifying the access level modifier:


class SomeInternalClass {}    //  implicit  internal
let someInternalConstant = 0   //  implicit  internal

conclusion

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


Related articles: