Swift 2.0 Study notes ( Day )--access level
Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog
Access level:
Swift provides 3 different access levels, with the corresponding access modifiers: public, internal, and private. These access modifiers can modify object-oriented types such as classes, structs, enumerations, and can also modify variables, constants, subscripts, tuples, functions, attributes, and so on.
L public. You can access any of the public entities in your module. If you use the import statement to introduce other modules, we can access the public entities in other modules.
L Internal. You can access only any internal entities of your own module, and you cannot access internal entities in other modules. Internal can be omitted, in other words, the default access limit is internal.
L Private. An entity that can be used only in the current source file, called a private entity. With the private adornment, you can use it as an implementation detail to hide certain features.
The example code that uses the access modifier is as follows:
Public class Publicclass {} Internal class Internalclass {} Private class publicvar0 0 Private
Use the best access level:
Due to the fact that the access qualifier in Swift can be decorated with many entities and is cumbersome to use, here are some best practices.
1. Principle of uniformity
Q Principle 1: If a type (class, struct, enumeration) is defined as internal or private, then the variable or constant declared by the type cannot use the public access level. Because public variables or constants can be accessed by anyone, the type of internal or private is not available.
Q Principle 2: The access level of a function cannot be higher than the access level of its parameter and return type (class, struct, enum). Assuming a function is declared as a public level, and the argument or return type is declared as internal or private, there is a contradiction that the function can be accessed by anyone whose arguments and return types are not accessible.
2. Design principles
If we 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, then we do not have to set the access level, that is to use the default access level.
If we are developing a framework, the framework compiled files cannot run independently, so it is inherently used by others, in which case we need to design the access level of swift files and entities in detail so that others can use them as public. Don't want to be seen by others can be set to internal or private.
3. Access levels for tuple types
The access level of a tuple type follows the level of access at the lowest level of a field in a tuple, such as the following code:
Private classEmployee {varNo:int =0 varName:string ="" varJob:string?varSalary:double =0 varDept:department?} structDepartment {varNo:int =0 varName:string =""} PrivateLet EMP =Employee ()varDept =Department ()Private varStudent1 = (Dept, EMP) ①
4. Enumeration types of access levels
The access level of a member in an enumeration is inherited from the enumeration, so we cannot specify an access level for the members in the enumeration. The sample code is as follows:
Public enum weekdays { case Monday case Tuesday case Wednesday Case Thursday Case Friday}
Because the weekdays enumeration type is the public access level, its members are also of the public level.
Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php
Swift 2.0 Study Notes (Day 29)--access level