This article and we share the main is the swift in the access control related content, come together to see, hope to learn swift to you to help. Access control constrains the access to your other code source files and modules section. This feature allows you to hide your code implementation and specify a 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 that belong to this type. Protocol can be limited to a context, global variables, variables, and functions. In addition, Swift also provides a default usage level for typical usage scenarios. Indeed, if you write a single target app, you probably don't need to explicitly specify access control levels at all.modules and source filesThe access control model for Swift is based on the concept of modules and source files. A module is a single code distribution unit ———— a framework or application is compiled and passed as a single unit, and they can be imported by other modules via Swift's import keyword. In swift, every build target of Xcode (such as an app bundle or framework) is treated as a separate module. While it is common practice to define different types in different source files, a source file can actually contain definitions of different types, functions, and so on. access level swift provides 5 different levels of access for your code entities: • open Access and public access allows entities to be used in any source file from a decision-making module, or from other imported modules. You typically use open or public to specify a public interface for framework . The different points of the two are described below. internal access allows entities to be used in any source file for which they define the model, but cannot be used by any source file outside the module. Usually use internal when defining an app or a framework 's internal structure Access. · file-private access restricts the use of entities in the definition source file. Use file-private access to hide implementation details for specific features when these details are used throughout the file. private access restricts the use of an entity to a closed declaration. Use private access to hide implementation details for specific features when these details are used in a single declaration. open access is the highest access level, private is the lowest access level (maximum restriction). open access is only used in classes and class members, and the difference between him and publick access is as follows: Use public Access class, or other more restrictive access levels, you can only create subclasses within a defined module. Class members that use public access, or other more restrictive access levels, can only be overridden within the defined module by its subclasses. open class can be defined module or other import the moduleWhere to create subclasses. • open class members can be overridden by defined modules or other subclasses of the module that are created in the import . In short, the difference between public and open is that public open has less permissions for class inheritance and class member overrides outside of the module. guidelines for access levelsIn Swift, the general guideline for access level compliance is that no entity can be defined in another entity that has a lower access level (more restrictions). For example: The public variable cannot be defined as having a internal, file-private, or private type, because this type may not be used anywhere in the public variable. A function cannot have a higher access level than other parameter types and return types, because the function can be used in situations where its constituent types are not used by the surrounding code. The following is a more detailed introduction.The default access levelIf you do not specify an explicit access level yourself, all entities in the code have a default internal access level. As a result, in many cases you do not need to specify an explicit level of access to your code.access levels for single-target applicationsIf you are writing an I simple single target should be the program, then your program code is a typical self-contained program, do not need to be used outside the program module. The default access level, internal, has been met for this requirement. Therefore, you do not need to specify an access level. However, you may need to mark your part of the code as private or private, allowing other code in the program module to hide their implementation details.frameworks level of accessWhen you develop a framework, mark open or public so that it can be accessed by other modules, such as when a program introduces the framework. This public-facing interface is the program programming interface (or API) of the framework. It is worth noting that the internal implementation details of any framework can also use the default internal access level, or can be identified as private or file private level if you want to hide them from other parts of the framework's internal code. Only if you want an entity to be part of your framework's API, then you need to identify the entity as open or public.the access level of the unit test targetWhen you write a program that contains a unit test target, then you need to let the code in your program be used by the test module for easy testing. In general, only entities that are identified as open or public can be accessed by other modules. However, if you add the module import declaration of the product to the @testable attribute and compile the product module under open Test options, the unit test target will be able to access any of the internal entities.access level SyntaxTo define an access level for an entity: PublicclassSomepublicclass{}InternalclassSomeinternalclass{}fileprivateclassSomefileprivateclass{}PrivateclassSomeprivateclass{} Publicvarsomepublicvariable = 0Internal LetSomeinternalconstant = 0fileprivatefuncsomefileprivatefunction() {}Privatefuncsomeprivatefunction() {} Unless otherwise specified, the default access is essentially internal, which means that Someinternalclass and someinternalconstant can also have internal without explicit access-level modifiers Access level:classSomeinternalclass{}//implicit internal LetSomeinternalconstant = 0//Implicit internal source: Mellong
Swift Learning access Control