Preface
Before Swift 3.0, there were three types of access control keywords, namely private, internal and public. After swift3, two access control keywords were added to the original basis: fileprivate and open. They can be seen as a further breakdown of private and public. Below are the differences between the individual modifiers and the access permission sort.
The difference between the modifiers
private
The properties or methods modified by swift3.0 private access level can only be accessed in the current class.
class A { private func test() { print("this is private function!") } } class B: A { func show() { test() } }
The above code can be successfully compiled successfully before swift3.0, but it will fail in swift3.0, prompting that the test() method in class B is not available.
fileprivate
fileprivate is a newly added permission modifier after Swift 3.0. The attributes or methods modified by the fileprivate access level can be accessed in the current Swift source file. (For example, in the above example, if private is changed to fileprivate, there will be no errors).
internal
Internal is the default access level and can be written by default. The properties or methods modified by the internal access level are accessible throughout the module where the source code is located. If it is framework or library code, it can be accessed inside the entire framework, and when the framework is referenced by external code, it cannot be accessed. If it is App code, it can also be accessed within the entire App code and within the entire App.
public
Can be accessed by anyone. However, other modules cannot be override and inherited, but can be override and inherited within modules.
open
Open is a new permission keyword after swift3.0, which can be used by anyone, including override and inheritance.
Modifier access sorting
From high to low permission control sequence is as follows
open > public > interal > fileprivate > private
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.