1, the private modifier

Private is only allowed to be called in the source file of the current class. Modified methods cannot be accessed outside of the code domain, except in extension, the source file of this class.

2. Fileprivate modifier

Fileprivate modifies properties or methods that are accessible only in the current Swift source file, that is, all fileprivate method properties are accessible in the same file

3. Public modifier

Modified properties or methods can be accessed in other scopes but cannot be accessed in override or in an Extension in an inherited method

4. Open modifier

Open makes up for the semantic deficiency of public. Now pubic has two meanings:

  • This element can be accessed in other scopes
  • This element can be inherited or override in another scope

Succession is a dangerous thing. This is especially true for a framework or Module designer. Within its own Module, a class or attribute is clear to the author, and can be inherited or overridden. But for those who use it, authors sometimes want to convey that the class or property should not be inherited or modified. This corresponds to final. The problem with final is that it cannot override anywhere after the tag. The lib designer wants to be able to override a module and not be able to override another user after being imported elsewhere. That’s where Open came from. The open and public tags distinguish whether an element can only be accessed in other modules or can be override.

5. The internal modifier

As the name implies, internal means internal, that is, properties and methods that have internal access rights are accessible inside the module, but not outside the module. In Swift, the default is internal access.

The order in ascending order is as follows: Open > Public > Interal > Fileprivate > Private

Reference: blog.csdn.net/shubinniu/a… www.jianshu.com/p/74a46c128…