Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use Public Access Control in Swift
Written by Team Kodeco

In Swift, the public access control level is the most permissive level of access control.

Public access allows a type or type member (e.g. property, method, initializer) to be accessed from any module or library. This means that any code, whether it’s within the same module or a different module, can access and use the public type or type member.

Here’s an example of a public class in Swift:

public class MyPublicClass {
  public var myPublicProperty = "I am a public property"
  public func myPublicMethod() {
    print("I am a public method")
  }
}

In this example, the MyPublicClass and its myPublicProperty and myPublicMethod are public and can be accessed and used by any code in any module.

© 2024 Kodeco Inc.