swift--Access Level-standby

Source: Internet
Author: User
Tags modifiers

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.

    • 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 .

    • 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.

    • 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:

    1. public class Publicclass {}
    2. Internal class Internalclass {}
    3. Private class Privateclass {}
    4. public var intpublicvariable = 0
    5. Let intinternalconstant = 0
    6. Private Func Intprivatefunction () {}

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

    • Principle 1: If a type (class, struct, enum) 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.

    • 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 can not run independently, so it is inherently for others to use, in this case we want to design in detail the Swift file and entity access level, let others use can be set to public , do not want others to see can be set as 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:

  1. Private class Employee {
  2. var No: Int = 0
  3. var name: String = ""
  4. var job:string?
  5. var Salary: Double = 0
  6. var dept:department?
  7. }
  8. struct Department {
  9. var No: Int = 0
  10. var name: String = ""
  11. }
  12. Private let emp = Employee ()
  13. var dept = Department ()
  14. private var student1 = (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:

    1. Public enum Weekdays {
    2. Case Monday
    3. Case Tuesday
    4. Case Wednesday
    5. Case Thursday
    6. Case Friday
    7. }

Because the Weekdays enumeration type is the public access level, its members are also of the public level.

swift--Access Level-standby

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.