乐闻世界logo
搜索文章和话题

What are the access control levels in Swift? What are the differences between open, public, internal, fileprivate, and private?

2月21日 15:06

What are the access control levels in Swift? What are the differences between open, public, internal, fileprivate, and private?

Swift provides five access control levels to control the visibility and accessibility of code entities (types, properties, methods, etc.). These access levels help developers achieve encapsulation and information hiding.

Access Control Levels:

  1. open:

    • Highest level of access permission
    • Can be accessed anywhere in the defining module
    • Can be accessed and inherited in other modules
    • Can be overridden in other modules
    • Mainly used for classes and class members
    • Example:
      swift
      open class OpenClass { open var openProperty: Int = 0 open func openMethod() {} }
  2. public:

    • Can be accessed anywhere in the defining module
    • Can be accessed in other modules
    • But cannot be inherited or overridden in other modules
    • Suitable for public APIs that shouldn't be inherited or overridden
    • Example:
      swift
      public class PublicClass { public var publicProperty: Int = 0 public func publicMethod() {} }
  3. internal (default):

    • Can be accessed anywhere in the defining module
    • Cannot be accessed in other modules
    • This is the default access level
    • Suitable for implementation details within a module
    • Example:
      swift
      class InternalClass { // defaults to internal var internalProperty: Int = 0 func internalMethod() {} }
  4. fileprivate:

    • Can only be accessed within the defining file
    • Cannot be accessed in other files of the same module
    • Suitable for auxiliary types and functions within a file
    • Example:
      swift
      class FilePrivateClass { fileprivate var filePrivateProperty: Int = 0 fileprivate func filePrivateMethod() {} }
  5. private:

    • Lowest level of access permission
    • Can only be accessed within the defining scope
    • Cannot be accessed in other types within the same file
    • Suitable for implementation details within a type
    • Example:
      swift
      class PrivateClass { private var privateProperty: Int = 0 private func privateMethod() {} func publicMethod() { privateMethod() // can access } }

Access Control Rules:

  1. An entity cannot be more public than its type:

    swift
    public class PublicClass { private var property: Int = 0 // correct // public var property: Int = 0 // incorrect }
  2. Tuple type access level is the most restrictive among all members:

    swift
    private var tuple: (Int, String) = (1, "hello") // private
  3. Function access level is the most restrictive among all parameter and return value types:

    swift
    private func process(_ data: InternalType) -> PublicType { // function access level is private }
  4. Nested type access levels:

    swift
    public class OuterClass { private class InnerClass { // InnerClass access level is private } }

Best Practices:

  1. Use internal by default, only use other levels when necessary
  2. Use private to restrict implementation details
  3. Use fileprivate for auxiliary functionality within a file
  4. Use public to define public APIs
  5. Use open for public APIs that need to be inherited and overridden
  6. Follow the principle of least privilege, only expose necessary interfaces
标签:Swift