Swift code organizes into modules—each target (app, framework, library) is a module with its own namespace. import brings another module’s public API into scope.
Import rules
import Foundation
// import UIKit // needs iOS SDK — use locally in Xcode
The playground allows Foundation; UIKit/SwiftUI require Apple SDK targets locally.
Access control
private— enclosing declarationfileprivate— same source fileinternal— same module (default)public/open— exported to clients
Important interview questions and answers
- Q: Module vs package?
A: Module is a compiled unit; Swift Package Manager groups modules into packages with dependencies. - Q: public vs open?
A:openallows subclassing and overriding outside the module;publicis read-only outside for classes.
Self-check
- What is the default access level?
- Why avoid UIKit import in sandbox code?
Tip: Default access is internal—use public only for API surface exported to other modules.
Interview prep
- Default access?
internal—visible within the same module.
- public vs open?
open allows subclassing outside module; public does not for classes.