Swift Module Organization
Introduction
Swift modules provide a powerful way to organize and structure your code. A module is a single unit of code distribution - a framework or application that is built and shipped as a single entity and that can be imported by another module. In this article, we'll explore how modules work in Swift, how they interact with access control, and how you can use them effectively to structure your code.
What is a Module in Swift?
In Swift, a module represents a code distribution unit. It can be:
- An application (your main app target)
- A framework (reusable code that can be imported by other projects)
- A library (compiled code that can be linked against)
Every Swift file you create belongs to a module. When you create an iOS app, all your Swift files belong to the app's module by default.
// This code is part of your app's module
class MyAppClass {
func doSomething() {
print("Hello from my app module!")
}
}
Importing Modules
To use code from other modules, you need to import them with the import
statement:
// Importing built-in modules
import Foundation
import UIKit
// Using types from imported modules
let date = Date()
let view = UIView()