Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

1. Access level

1.1. The open

Open is by far the most powerful, accessible in permitted entity modules, other modules, and allows other modules to inherit and overwrite.

For example: TargetA has classA, the permission is open, classB in TargetB can inherit classA, classA methods, member variables and so on can be accessed.

1.2. The public

Public and Open are similar in that they allow access to entity modules, other modules, except that they do not allow other modules to inherit or overwrite.

For example, if the testB method in the TargetA is classA, and the testB method is testA, and the permission is public, then the testB method in the classB can initialize var a = classA() and call a.testa ().

1.3. Internal

Internal allows access only in defined entity modules, not in other modules. This is also the default permission for many entities.

1.4. Fileprivate

Fileprivate translates to file privacy and allows access only in defined files.

For example, in a Target, there are two classes classA and classB in two files, classA’s current permission is Fileprivate, so classB cannot access classA. If classA and classB are in the same file, they can be accessed.

1.5. Private

Private allows access only within the currently defined entity.

For example: classA and classB are in the same file, classA permission is private, then classB can not access classA in principle. In order to access, you need some information. We’ll talk about that later.

2. Guidelines for the use of access levels

An entity may not be defined by an entity of a lower access level.

2.1. Access level of variable type >= Access level of variable

For example: define a class fileprivate class ClassA{}. If you define it as internal var ClassA: ClassA, an error will be reported. The entity type of the permission ClassA must be greater than the variable ClassA.

2.2. Parameter type, return value type >= function

For example: func testA(_ num: Int) -> Double {}, the access level of the function is internal by default.

2.3. Superclass >= subclass

If I can access a subclass, the parent class should also be accessible. For example: class SupClassA {}, the subclass class SupClassA: SupClassA {}, the default parent class is internal, then the subclass cannot be public and open.

3. Members have nested types

3.1. The type is private and Fileprivate

When the type is private,fileprivate, the default member type is also private or Fileprivate

For example: flieprivate class ClassA {var a = 0, var b = 0}, both a and B are fileprivate by default.

fileprivate class ClassA {
    var a = 0
    private var b = 0
}
Copy the code

3.2. The type is internal, public

When the type is internal, public, the default member type is internal

public class ClassA {
   internal var a = 0
}
Copy the code

4. The getter and setter permissions

For reading and writing, there are many times when we want others to read our values without allowing them to change them. We can define this as follows:

class ClassA {
   private(set) var age: Int = 0
}
Copy the code

5. Test

5.1. Case 1

private class Person {}

fileprivate class Student: Person {}

Does this example compile?

This is where you can see that it can pass depending on the scope.

1Person is scoped as classA, so Student cannot inherit from Person.

The scope of 2Person2 is the current file, and Student2 is the current file, so it can be inherited.

5.2. Case 2

private class ClassA { var age: Int = 0 func testA() {} } fileprivate class ClassB { var classA = ClassA() func testB() { classA.age = 2 classA.testA() }}Copy the code

TestA = testA; testA = testA; testA = testA; age = testA;

Just look at the results.

ClassA is private and subclasses are private by default.

So, in ClassB, you can define classA, and you can say that globally, classA is also fileprivate, and age and testA are fileprivate, and if you change it to private, you’re scoped in classA, So an error will be reported.

At this point we can conclude that private defined directly in global scope is equivalent to Fileprivate.