Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use Internal Access Control in Swift
Written by Team Kodeco

The internal access level is the least restrictive level of access control in Swift.

It’s also the the default level of access in Swift. This means when you don’t specify an access level explicitly, the code is considered internal.

The internal access level allows access to code within the same module. A module is a unit of code distribution, such as a framework or an app.

Here’s an example of a class that has an internal property and an internal method:

class MyClass {
  internal var myInternalProperty = "I'm internal"
  internal func myInternalMethod() {
    print("I'm an internal method")
  }
}

In this example, myInternalProperty and myInternalMethod can be accessed from within MyClass, as well as any other scope within the same module as MyClass. If you try to access them from outside the module, you’ll get a compile-time error.

Remember that since internal is the default access level, there is no need to manually specify it:

class MyClass {
  var myInternalProperty = "I'm internal"
  func myInternalMethod() {
    print("I'm an internal method")
  }
}

In this example, both myInternalProperty and myInternalMethod are internal since that is the default.

© 2024 Kodeco Inc.