Access control in Swift & Top 5 interview questions related to access control

Nitish Kumar
3 min readNov 6, 2023

Access control in Swift is a feature that allows you to restrict the visibility and usage of classes, structs, enums, properties, methods, and other entities within your codebase.

Here’s a brief summary of each access control level:

  • Open: Can be accessed, subclassed, and overridden from anywhere.
  • Public: Can be accessed within the same module and from other modules, but cannot be subclassed or overridden outside the module.
  • Internal: Can be accessed within the same module but not outside the module.
  • File-Private: Can be accessed only within the same file.
  • Private: Can be accessed only within the same enclosing declaration (such as a class, struct, or enum).

Top 5 interview questions related to access control in Swift along with their answers:

1. Question: What are the different access control levels in Swift and when would you use each one?

Answer:
- Open : Use open when you want a class to be accessible from any source file and subclassed or overridden outside the module.
- Public: Use public when you want an entity to be accessible from any source file within the same module and from other modules that import the module where the entity is defined. However, it cannot be subclassed or overridden outside the module.
- Internal : Use internal when you want an entity to be accessible within the same module but not outside the module.
- File-Private: Use fileprivate when you want an entity to be accessible only within the same source file.
- Private: Use private when you want an entity to be accessible only within the same enclosing declaration (such as a class, struct, or enum).

2. Question: What is the default access control level in Swift, and how does it impact your code?

Answer: The default access control level in Swift is `internal`. If you do not specify an access control level explicitly, Swift assigns `internal` by default. It means entities are accessible within the same module but not from outside the module. This default promotes encapsulation and modular design within your codebase.

3. Question: What is the difference between `open` and `public` access control levels?

Answer: Both `open` and `public` allow access from any source file within the same module and from other modules that import the defining module. The key difference is that `open` can be subclassed and overridden outside the module where it’s defined, whereas `public` cannot be subclassed or overridden outside the defining module.

4. Question: Can a `private` variable be accessed from an extension of the enclosing type?

Answer:Yes, a `private` variable can be accessed from an extension of the enclosing type as long as the extension is defined in the same source file as the type itself. Extensions in the same file have access to `private` members of the extended type.

5. Question: When would you use `fileprivate` over `private` access control level?

Answer: You would use `fileprivate` when you want an entity to be accessible within the same source file but not from outside that file. This level of access control allows you to hide implementation details within a single file, encapsulating the behavior and state of your types.

--

--

Nitish Kumar

I developed and maintain applications aimed at a range of iOS devices including mobile phones and tablet computers.