Swift Modules

You write software in files.

A simple program might be stored in a single file, but complex programs are written across multiple files.

Swift provides a way to group several files into a group, called module.

Modules helps us do 2 things: r…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

You write software in files.

A simple program might be stored in a single file, but complex programs are written across multiple files.

Swift provides a way to group several files into a group, called module.

Modules helps us do 2 things: reuse code, and encapsulate code.

You just have to write a particular functionality once, and after putting it into a module, you can import that into different places and projects.

Encapsulation means that the library can do lots of complicated things internally, but you only expose a tiny bit of it to the outside.

You start using modules by importing them.

If you’ve ever written

import SwiftUI

or

import UIKit

you’ve already used modules. Frameworks like UIKit and SwiftUI, and many others, are modules.

After you import a module, everything that that module declares public will be visible inside your application code.

Modules can import other modules, and when this happens you have access to those modules automatically.

For example SwiftUI imports Foundation, so you don’t have to write

import SwiftUI
import Foundation

because you just need the first line.

Your application is a module, too.

Swift itself is a module, as well. And you never have to import Swift because it’s done automatically for you.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-09-12T05:00:00+00:00) Swift Modules. Retrieved from https://www.scien.cx/2021/09/12/swift-modules/

MLA
" » Swift Modules." flaviocopes.com | Sciencx - Sunday September 12, 2021, https://www.scien.cx/2021/09/12/swift-modules/
HARVARD
flaviocopes.com | Sciencx Sunday September 12, 2021 » Swift Modules., viewed ,<https://www.scien.cx/2021/09/12/swift-modules/>
VANCOUVER
flaviocopes.com | Sciencx - » Swift Modules. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/12/swift-modules/
CHICAGO
" » Swift Modules." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/09/12/swift-modules/
IEEE
" » Swift Modules." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/09/12/swift-modules/. [Accessed: ]
rf:citation
» Swift Modules | flaviocopes.com | Sciencx | https://www.scien.cx/2021/09/12/swift-modules/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.