78

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?

timbo
  • 13,244
  • 8
  • 51
  • 71
sprinter
  • 27,148
  • 6
  • 47
  • 78
  • 12
    Off-topic? Seriously? There is presently no known resources for modules in swift, helping. Others find information is extremely valuable. This isn't subjective because nothing yet exist! – Brett Ryan Jun 12 '14 at 04:00
  • I have some finding recently on how to import modules (C, C++) into Swift using [Swift Compiler - Search Paths](http://stackoverflow.com/a/24794675/2920335) (Xcode 6 Build Settings). Hope it helps. – vladof81 Jul 17 '14 at 18:39

5 Answers5

26

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.

Austin
  • 5,625
  • 1
  • 29
  • 43
  • 1
    Thanks for this answer. I had gathered from the keynote address that one of the major issues in ObjC that Swift was addressing was the ability to modularise code by using namespaces. I'm sure they intend for that to refer to something other than creating frameworks for each module - that would be a lot of overhead and would not support a hierarchy of modules. To be honest this feels like it's been rushed out to market unfinished. I'm sure a number of these features will be added quickly and it's probably worth us developers waiting a couple of releases before using it. – sprinter Jun 04 '14 at 00:44
  • 1
    Yeah, I had asked about this same thing, and the engineer said it was one of their most requested features. So it's definitely on their radar – Austin Jun 04 '14 at 00:46
  • 1
    Right now you can do that by making a class with member classes. I know its not the same thing, but it functions in much the same way. – o.uinn Aug 05 '14 at 22:21
8

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

AlexDenisov
  • 4,022
  • 25
  • 31
  • Nice information. This can also link without Xcode, or REPL. Though it's freaky that can't make `swiftmodule` using `--sdk iphoneos`. – vladof81 Jul 17 '14 at 18:53
  • You can use SWM (Swift Modules) to create the importable `.swiftmodule` binaries – http://github.com/jankuca/swm – J. K. Dec 13 '14 at 11:03
-2

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.

elito25
  • 624
  • 1
  • 6
  • 14
-3

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.
Clement Prem
  • 3,112
  • 2
  • 23
  • 43
  • 33
    How does this answer help someone create an importable module in Swift? – Andrew Paul Simmons Sep 08 '17 at 15:14
  • Frameworkizing of the project will reduce startup performance. Each framework needs time to load. – kelin Aug 26 '18 at 11:45
  • Yes, dynamic linking will slow down app launch. Apple suggest a threshold of 6 dynamic frameworks. But in Xcode 9 you can change the linking to static library in Mach-O Type to address the startup issue. For more info https://developer.apple.com/videos/play/wwdc2016/406/ – Clement Prem Aug 27 '18 at 15:21
-4

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.”
luics
  • 85
  • 6