How do I import a Swift file from another Swift file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Inside the same Swift target or module, you do not import one .swift file from another .swift file. Swift compiles the files in a module together, so types and functions are already visible to each other as long as access control and target membership allow it.
Same Module: No File Import Needed
Suppose you have two files in the same app target.
MathUtils.swift:
ContentView.swift:
There is no import MathUtils.swift. The file is already part of the same compiled module.
What You Actually Import in Swift
Swift imports modules, not source files. Examples:
If you created your own framework or package module, then you import that module name:
That is the correct mental model. File-level imports are not the mechanism.
When Code Is Not Visible
If one Swift file cannot see another file's types, the issue is usually one of these:
- The files are in different targets
- The symbol has restrictive access control such as
privateorfileprivate - The framework or package module was not imported
So the fix is not "find the file import syntax." The fix is usually to correct module boundaries or visibility.
Access Control Matters
These access levels affect visibility:
- '
privateandfileprivatecan block access outside the file or scope' - '
internalis the default and works inside the same module' - '
publicandopenare for access from other modules'
Example:
That type cannot be used from another file, even in the same module, because fileprivate restricts it to the same source file.
Check Target Membership in Xcode
Another common issue is that the file exists in the project navigator but is not included in the target that is being built. In Xcode, check the File Inspector and confirm the file belongs to the target that needs it.
This problem often looks like a missing import, but it is really a build-configuration issue.
Swift Package and Framework Case
If the code lives in another Swift package or framework, then module import is correct:
In that case:
- The other code must be built as a separate module
- The symbols must be exposed with appropriate access control
- Your current target must depend on that package or framework
That is the only time the import statement changes: when the code boundary becomes a module boundary instead of just another file in the same build target.
Common Pitfalls
- Trying to write an
importstatement for a source file name. - Using
privateorfileprivateand then assuming the problem is import-related. - Forgetting target membership in Xcode.
- Confusing files with modules. Swift imports modules, not individual files.
Summary
- You do not import one Swift file into another within the same module.
- Files in the same target are compiled together and can use
internalsymbols automatically. - Import is for modules such as frameworks, packages, and system libraries.
- If code is not visible, check access control and target membership first.
- In Swift, the real boundary is the module, not the file name.
Related reading
- How do I inspect the view hierarchy in iOS?
- How do I launch the Android emulator from the command line?
- How do I load an HTTP URL with App Transport Security enabled in iOS 9?
- How do I locate the CGRect for a substring of text in a UILabel?
- How do I make a custom initializer for a UIViewController subclass in Swift?
- How do I make a dotted/dashed line in Android?
- How do I make a new line in swift
- How do I make an attributed string using Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.