How to Choose Between open and public Access Control in Swift

Written by

in

Quick answer: Use open when you want a class or member to be subclassed or overridden outside its defining module. Use public when you want it accessible but not overridable externally. open is the least restrictive access level; public allows access but prevents external inheritance.

Why this happens

Swift 3 introduced the open access level via SE-0117 to separate visibility from overridability. Before Swift 3, public meant both “visible everywhere” and “overridable everywhere.” This created a problem for framework authors: they couldn’t expose API publicly without also allowing external subclasses to override internal implementation details.

The change makes the intent explicit. public now means “accessible from any module” but “not subclassable or overridable outside the defining module.” open means “accessible and overridable from any module.” This distinction only applies to classes and class members (methods, properties, subscripts). Structs, enums, protocols, and their members cannot be open because they don’t support inheritance.

How to fix it

1. Default to public for most framework API

When designing a library or framework, mark classes and members public by default. This lets consumers use your types while preventing them from overriding behavior you didn’t intend to be customized.

public class NetworkClient {
    public let baseURL: URL

    public init(baseURL: URL) {
        self.baseURL = baseURL
    }

    public func send(_ request: URLRequest) async throws -> Data {
        // Internal implementation
        let (data, _) = try await URLSession.shared.data(for: request)
        return data
    }
}

Consumers can instantiate NetworkClient and call send(_:), but they cannot subclass it or override send(_:) in their own module.

2. Use open deliberately for extension points

Mark a class open only when you intend it to be subclassed externally. Mark a member open only when you intend it to be overridden. This is common for base classes designed for inheritance, such as view controllers, view models, or plugin architectures.

open class BaseViewController: UIViewController {
    open func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    open func setupUI() {
        // Override in subclasses
    }
}

// In a different module:
class CustomViewController: BaseViewController {
    override func setupUI() {
        view.backgroundColor = .systemBlue
    }
}

3. Combine public classes with open members when needed

A public class can have open members. This allows external subclasses (if the class were open) to override specific members, but since the class itself is public, external subclassing is still prohibited. This combination is rarely useful; typically you either make the class open or keep everything public.

public class Logger {
    // This member is open but the class is public — 
    // external modules can't subclass Logger, so this open is ineffective.
    open func log(_ message: String) { 
        print(message) 
    }
}

4. Remember: open implies public

open is strictly more permissive than public. An open class is accessible everywhere (like public) and subclassable everywhere. An open member is accessible everywhere and overridable everywhere. You never need to write open public.

How to prevent it

  • Audit your framework’s public API: After adding a new class, ask “Should external code subclass this?” If no, keep it public.
  • Document extension points: When you do use open, add a comment explaining why and how it should be overridden.
  • Use final for leaf classes: If a public class should never be subclassed even within your own module, mark it final.
  • Test from a separate module: Add a test target that imports your framework and verify subclassing/overriding behaves as expected.

FAQ

Can a struct or enum be open?

No. open only applies to classes and class members (methods, properties, subscripts, initializers). Structs, enums, protocols, and their members use public as their least restrictive level because they don’t support inheritance.

What happens if I override a public method in another module?

The compiler emits an error: "Method cannot be overridden because it is not marked open." You must either change the original method to open (if you control the module) or use composition instead of inheritance.

Does open affect performance or binary size?

No. open is a compile-time access control check. It has no runtime overhead. The compiler uses it to enforce module boundaries; the generated code is identical to public.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.