Swift Access Limits

Source: Internet
Author: User


The swift language can be encapsulated, and because of the object-oriented nature of classes, structs, and enumeration types in Swift, Swift's encapsulation becomes more complex


I. Scope of Access


There are 2 main access scopes: modules and source files


1, about the module

A module is an application package into a framework in which the Import keyword can be used to introduce a module into its own project


An application package refers to an executable application package that contains many swift files and other files inside it.


The framework is also a collection of many SWITF files and other files, but unlike the application package, it compiles the result of a non-executable program


2. Source Files


The source file refers to Swift's. Swift file, which is compiled and then included in the application package or framework, typically a source file that contains an object-oriented type (class, struct, and enumeration) that contains functions, attributes, and so on


Ii. level of Access


Swift provides 3 different levels of access, with these modifiers: public,private,internal, which can be decorated with object-oriented types such as classes, structs, nesting, and can also be modified: variables, constants, subscripts, tuples, functions, attributes, and so on


1. Public can access any public entity in its own class, and if you use import to introduce other modules, you can access the public entities of other modules


2, internal can only access the internal entity of its own module, cannot access the internal entities of other modules, internal can be omitted, the default access limit is internal


3. Private entities that can only be used in the current source file become private entities, using the private modifier, which can be used as an implementation detail to hide certain features.


Iii. use of access restriction best practices


1. Principle of unity

(1) Principle 1: If a type (class, struct, and enumeration) is defined as internal or private, then the variable or constant declared by the type cannot use the public access level, because a variable or constant of public allows access by anyone, The private or internal type does not allow

        private class Employee {                 var no:int = 0                 var name: string =  ""                  var job:String?                 var salary:Double                 var dept:department?}         internal struct Department {                 var no: int =  0             &Nbsp;  var name:string = ""}    public let emp =  Employee ()  //compilation error     public var dept = department ()  //compilation error


(2) Principle 2: The access level of a function cannot be higher than the access level of its parameter and return type, assuming that the function is declared public, and the parameter or return value is declared as internal or private, the function can be accessed by anyone, but its arguments and return type cannot be contradictory


Example:

        class Employee {                 var no:Int = 0                 var name:string =   ""                 var  job:String?                 var salary:Double                 var dept:department?}         struct Department {                 var no: Int = 0                 var nAme:string = ""}        public func getempdept (EMP: Employee)->department? {                             return emp.dept         }        the above code will compile errors, Indicates that the access level of the Getempdept function is higher than the access level of the employee type


2. Design principles


If you are writing an application, all swift files in the application package and the entities defined therein are for use by the application, not for other modules, so you do not have to set the access level or the default access level.

If you are developing a framework, the framework-compiled file cannot run independently, so it is used by others, and in this case we need to design the access level of swift files and entities in detail, where others use the design as public, You don't want anyone to see it. can be designed as internal or private


3. The access level of the tuple type


The access level of a tuple type is the access level that follows the lowest level of the field in its tuple, as shown in the following example:


       private class Employee {                 var no:Int = 0                 var name:string  =  ""                  var job:String?                 var salary:Double                 var dept:department?}         struct Department {                 var no: Int = 0                 var name:string = ""}        public func getempdept ( Emp:employee)->department? {                             return  emp.dept        }                 private let emp = employee ()          var dept = department ()                  private var student1 =  (Dept, Emp

4. Enumeration type of access level


The access level of a member in an enumeration is inherited from the enumeration, and we cannot specify the access level for the members in the enumeration, as shown below

Public enum weekdays{Case Monday case Tuesday case Wednesday Case Thursday Case Friday}

The access level of weekdays is public, and its members have access levels of public



This article is from the "Ordinary Road" blog, please be sure to keep this source http://linjohn.blog.51cto.com/1026193/1621427

Swift Access Limits

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.