Privacy (also known as access control) refers to the explicit modification of the normal scope rules. I gave an example in Chapter 1:
class Dog {
var name = ""
private var whatADogSays = "woof"
func bark() {
print(self.whatADogSays)
}
}
The intention here is to limit how other objects can see the Dog property whatADogSays. It is a private property, intended primarily for the Dog class’s own internal use: a Dog can speak of self.whatADogSays, but other objects should not be aware that it even exists.
Swift has five levels of privacy:
internal
The default rule is that declarations are internal, meaning that they are globally visible to all code in all files within the containing module. That is why Swift files within the same module can see one another’s top-level contents automatically, with no effort on your part. (That’s different from C and Objective-C, where files can’t see each other at all unless you explicitly show them to one another through include or import statements.)
fileprivate (narrower than internal)
A thing declared fileprivate is visible only within its containing file. For example, two object types declared in the same file can see one another’s members declared fileprivate, but code in other files cannot see those members.
private (even narrower than fileprivate)
A thing declared private is visible only within its containing curly braces. In effect, the visibility of an object type’s member declared private is limited to code within this type declaration. (A private declaration at the top level of a file is equivalent to fileprivate.)
public (wider than internal)
A thing declared public is visible even outside its containing module. Another module must first import this module before it can see anything at all. But even when another module has imported this module, it still won’t be able to see anything in this module that hasn’t been explicitly declared public. If you don’t write any modules, you might never need to declare anything public. If you do write a module, you must declare something public, or your module is useless.
open (even wider than public)
If a class is declared open, code in another module can subclass it; it can’t do that if the class is declared merely public. If an open class member is declared open, code in another module that subclasses this class can override this member; it can’t do that if the member is declared merely public