sprinter
sprinter

Reputation: 27966

How do I create an importable module in Swift?

I have read through Apple's documentation for Swift and can find nothing about how to create modules or how to define class or stucture members as private or public.

There are references to the import statement in the syntax but I can find no information on what it does or how to use it.

Does anyone know where I can find this?

Upvotes: 79

Views: 40418

Answers (5)

Clement Prem
Clement Prem

Reputation: 3119

Update

You can modularize a swift project using frameworks.

We modularize by creating separate framework projects for each module and link them via Xcode workspace. It looks more natural when we separate the components into different projects & it also makes sure that there is only a one-way communication between modules. Developers can work/test on isolation without thinking much about other modules.

By default classes/structs/etc created within a framework will have an internal access control type so it is only visible within the modules. In order to make it visible outside the module, you can make it public.

More info on access controll level here


The latest Xcode 6 beta update (beta 4) bring access control to swift

Swift Enables Access Control

Swift access control has three access levels:

  • private entities can only be accessed from within the source file where they are defined.
  • internal entities can be accessed anywhere within the target where they are defined.!
  • public entities can be accessed from anywhere within the target and from any other context that imports the current target’s module.

Upvotes: -3

luics
luics

Reputation: 95

Swift 4.0

Description from the chapter "Access Control" in the Apple book "The Swift Programming Language (Swift 4 Edition)"

Swift provides five different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.

  • open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework.
  • internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
  • fileprivate access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
  • private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.”

Upvotes: -4

AlexDenisov
AlexDenisov

Reputation: 4117

Also, there is a way to make a module by yourself, but it's a bit harder way.

If you'll look at xcrun swift -help you may see a few options, and there are -emit-module, -emit-library and -emit-object which might be useful, but, probably, you should prefer official way and distribute modules via Frameworks.

If you still want to make module on your own, you can read this guide with some explanation

Upvotes: 8

elito25
elito25

Reputation: 642

Apple mentioned that private methods don't exist yet but they are in the process of being implemented. Remember that this is a newborn language and it is still being build up.

Upvotes: -2

Austin
Austin

Reputation: 5655

In Swift, "Modules" refers to Frameworks. Xcode now has a template for creating a framework project for both iOS and OS X.

There is currently no way to declare methods or properties public / protected. If you would like to see this added as a feature, you can make a feature request on Apple's bug reporter. It should also be noted that Apple has stated that the language could change with each release of Xcode, so it is possible that member access levels could be added before the public release.

Upvotes: 26

Related Questions