Swift

Access Control

Control where variables go

What is Access Control?

Access Control lets us tell which data inside types to be accessible from the outside code. There are four modifiers:

  • Public: Everyone can read and write
  • Internal: Only your code can read and write, others cannot.
  • File Private: Only Swift code in the same file the type is in can read and write. (write fileprivate)
  • Private: Most secure, only accessible by type methods and extensions.

Most often, you will never need to do this. Only sometimes you will need to set variables to private so they cannot be accessed by others or from other areas of the code. This is helpful to make sure everything is safe and nothing breaks. Here is an example of it’s usage.

class Car {
    private var color: String?
}
chevron_up