14

I need to create a static library with Swift, and I need to know how can I implement interface for the library.

In Objective-C I can mark needed headers as public in build phases, but there is not any headers and any interfaces in Swift.

What should I do with Swift?

2
  • 1
    Or in other words: When Apple releases Cocoa Touch written natively in Swift, how will they prevent me from seeing their implementation source code? Commented Dec 21, 2014 at 21:28
  • Here is a Build Phase script you could use. gist.github.com/brennanMKE/ad8f68524aac6877ef1f277a820d335d Commented Nov 6, 2019 at 21:35

2 Answers 2

25

Simply put: you don't.

Swift is not a language that separates headers and implementations. When you create a library or framework based on Swift and only for consumption by Swift, the Xcode default build setting of DEFINES_MODULE already does the job for you. This will create a .swiftmodule file, which will be used by import in other Swift projects.

If you want your code to be importable from Objective-C though, you might want to check if the SWIFT_INSTALL_OBJC_HEADER build setting is also enabled (which it is by default for frameworks as far as I know). Then the Swift compiler will generate a <ProductName>-Swift.h file for you, which you can import in Objective-C code to access your Swift classes and functions.

Sign up to request clarification or add additional context in comments.

2 Comments

Did this change with XCode beta 5? I can't find a .swiftmodule file anywhere in my project (Cocoa Touch Library with Swift)
Is there a way to have Xcode generate a <FileName>-Swift.h file for each swift file or object, instead of one file with all of them lumped together?
7

If you want your Swift framework to expose certain classes to the interface, simply mark the 'entities' (functions, variables etc.) public:

public class Class {} public var variable: Int public func function() { } 

By default, all entities have internal access.

  • public entities are intended for use as API, and can be accessed by any file that imports the module, e.g. as a framework used in several of your projects.
  • internal entities are available to the entire module that includes the definition (e.g. an app or framework target).
  • private entities are available only from within the source file where they are defined.

Source: the official Swift blog.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.