What does a module mean in swift?
A module is a single unit of code distribution—a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.
Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift. If you group together aspects of your app’s code as a stand-alone framework—perhaps to encapsulate and reuse that code across multiple applications—then everything you define within that framework will be part of a separate module when it’s imported and used within an app, or when it’s used within another framework.
As the docs indicate, the module is an application or a framework (library). If you create a project with classes A
and B
, they are part of the same module. Any other class in the same project can inherit from those classes. If you however import that project to another project, classes from that another project won't be able to subclass A
nor B
. For that you would have to add open
indicator before their declarations.
Basically, if you work on a single app then you are working in one single module and unless declared as private
or fileprivate
, the classes can subclass each other.
EDIT
Let us have following class in module (project) Module1:
class A {
}
Since this class is not open
, it can be subclassed only within the same module. That means that following class:
class B: A {
}
Can be written only in the same project, in Module1.
If you add Module1 as a dependency to project Module2, and try to do this:
import Module1
class C: A {
}
It will not compile. That's because class A
is not open
(in other words it has access public
or less) and it does not belong to the same module as C
. A
belongs to Module1, C
belongs to Module2.
Note
import
keyword imports a dependency module to your current module. If you write import UIKit
in your project, you are telling the compiler that you want to use module UIKit
in your module. import
does not define current module. Current module is the current project.
Adding import UIKit
at the beginning of the file does not change nor define to which module the file belongs. It just tells the compiler that in that file you want to use code from UIKit
module.
What is the difference between a clang (objective-C) module and a Swift module?
They are different. At the end of the build process though, they both need to be linked to your application/ library's other .o
and .dylib
files for it to run.
Swift modules
From Swift Serialization.md docs:
The fundamental unit of distribution for Swift code is a module. A module contains declarations as an interface for clients to write code against.
Swift acccess control docs:
A module is a single unit of code distribution: a framework or application that’s built and shipped as a single unit and that can be imported by another module with Swift’s import keyword.
Configured by
.target()
's inPackage.swift
Cannot have submodules, so users cannot
import Module.Submodule
in Swift. Users can still import specific entities,import struct PackageModel.Manifest
, but this is a lot more verbose than importing submodules.Its interface exists as a
.swiftmodule
. What is a .swiftmodule
?. The documentation says:Conceptually, the file containing the interface for a module serves much the same purpose as the collection of C header files for a particular library.
The compiler produces this
.swiftmodule
file a lot, like a generated objective-C header, but instead of text, its a binary repesentation. It includes the bodies of inlinable functions, much like static inline functions in objective-C or header implementations in C++. However, Swift modules does include the names and types of private declarations. This allows you to refer to them in the debugger, but it does mean you shouldn't name a private variable after your deepest darkest secret. from WWDC 2018: Behind the Scenes of the Xcode Build Process- So private declarations are exposed in your
.swiftmodule
(Swift module interface).
- So private declarations are exposed in your
When importing pure Objective-C frameworks into Swift, the Swift compiler uses its built-in clang compiler to import an Objective-C header.
The importer finds declarations in the headers exposed in Clangs
.modulemap
for that framework. (again, from WWDC2018)
- When importing Objective-C + Swift frameworks into Swift, the Swift compiler uses the Umbrella header.
Clang modules
- Configured by
YourModuleName.modulemap
file (previouslymodule.map
, but this is deprecated), formatted like this - Can have submodules, e.g.
std
module hasstd.io
andstd.complex
. - A clang module exposes header files specified in the module map. Private details (in
.m
) are not exposed at all. - Is an improvement of the original
#include
or#import
style imports to improve the build process (This is a big topic, read the Clang module docs).
what is the purpose of module in IOS interface builder?
When you need to use a custom class that its source file is embedded in .framework file for example.
let's say you've added a cocoapod library that gives you a custom UIView subclass and you want to add it inside your storyboard. So you will have to "import" that library to IB and then select the custom view.
You can think of it as the "import Library" line for the IB.
How to define a class and module for a view created programmatically in swift
Simply instantiate it instead of UIView
.
Assuming this is your custom view:
class MyCustomView: UIView {
//...
}
Here is how to instantiate it:
let myView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)
The module is the module your source file of class MyCustomView: UIView...
is member of. When developing an iOS application (not a framework or other target types) this is your app. You can choose the "Target Membership" in Xcodes Inspector when selecting a source file:
Related Topics
Real Time Nstask Output to Nstextview With Swift
What Does It Mean That String and Character Comparisons in Swift Are Not Locale-Sensitive
Nsurlsession Concurrent Requests With Alamofire
Photopicker Discovery Error: Error Domain=Pluginkit Code=13
The "++" and "--" Operators Have Been Deprecated Xcode 7.3
Send Data from Tableview to Detailview Swift
Why Optional Constant Does Not Automatically Have a Default Value of Nil
What Is the 'Open' Keyword in Swift
Non-'@Objc' Method Does Not Satisfy Optional Requirement of '@Objc' Protocol
Initialize @Stateobject With a Parameter in Swiftui
Deleting List Elements from Swiftui'S List
How to Generate a Random Number in a Range (10...20) Using Swift
Get Class Name of Object as String in Swift