Swift programming
file import
Swift development
code organization
programming tutorials

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.

Browse interview questions

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:

swift
1struct MathUtils {
2    static func double(_ value: Int) -> Int {
3        value * 2
4    }
5}

ContentView.swift:

swift
1import SwiftUI
2
3struct ContentView: View {
4    var body: some View {
5        Text("\\(MathUtils.double(21))")
6    }
7}

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:

swift
import Foundation
import UIKit
import SwiftUI

If you created your own framework or package module, then you import that module name:

swift
import NetworkingKit

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 private or fileprivate
  • 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:

  • 'private and fileprivate can block access outside the file or scope'
  • 'internal is the default and works inside the same module'
  • 'public and open are for access from other modules'

Example:

swift
fileprivate struct HiddenHelper {
    static func value() -> Int { 42 }
}

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:

swift
import MyUtilities

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 import statement for a source file name.
  • Using private or fileprivate and 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 internal symbols 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.