Static Library and Swift

Swift consumer -> Swift static library

Xcode version 10.2.1

Create Swift static library

Create a library project or create a library target

File -> New -> Project... -> Cocoa Touch Static Library
//or
Project editor -> Add a Target -> Cocoa Touch Static Library 

Add files .swift

Select `.swift` file -> Select File Inspectors Tab -> Target Membership -> Select the target
//or
Project editor -> select a target -> Build Phases -> Compile Sources -> add files

Build library - ⌘ Command + B or Product -> Build

Note 1: Be sure that you build library for the same process architecture as the client code.
Note 2: expose your API that should be visible for consumer using public or open access modifiers[About]

Find generated output[Build location]

Products group -> lib<product_name>.a -> Show in Finder

The directory includes

  • lib<product_name>.a – a built static library
  • <product_name>.swiftmodule. swiftmodule describe an interface of a library and a compiler version. This folder includes:
  • .swiftdoc - docs
  • .swiftmodule - public interface/definitions

Swift consumer with Swift static library

Drag and drop the binary into the Xcode project[About]

Link Binary[Undefined symbols] [Link vs Embed]

Project editor -> select a target -> General -> Linked Frameworks and Libraries -> add -> Add Others... -> point to `lib<target_name>.a` file
//or
Project editor -> select a target -> Build Phases -> Link Binary With Libraries -> add -> Add Others... -> point to `lib<target_name>.a` file

Add Library Search paths(LIBRARY_SEARCH_PATHS)[Library not found for] [Recursive path]

Project editor -> select a target -> Build Settings -> Search Paths -> Library Search paths -> add path to the parent of `lib<target_name>.a` file

Add Import Paths[No such module] [Recursive path]

Project editor -> select a target -> Build Settings -> Swift Compiler - Search Paths -> Import Paths -> add path to a folder with `.swiftmodule`

Import module to the Swift client code [module_name]

import module_name

[More examples]


As of Xcode 9 beta 4, Xcode natively supports static libraries with Swift sources.


As mentioned, Apple does allow Swift in static libraries as of Xcode 9 Beta 4.

We attempted to do this on an existing project with an Objective-C-based target and "child" static library projects and kept running into a linking error

ld: library not found for -lswiftSwiftOnoneSupport for architecture x86_64

also

ld: library not found for -lswiftDispatch for architecture x86_64

This is because the main target (app) is trying to build solely against Objective-C and isn't told by the static library that it needs to include Swift libraries. This was because there weren't any Swift files in the Compile Sources section of our Build Phases for the app target.

So basically all you have to do is add at least one .swift file to that compile list and it will include the Swift libraries for you. It doesn't even need to have any code or values in it, it can be an empty file.

Then you can start adding Swift files to your "child" static library project. I would let it generate the bridging header for you at first then you can move it around and change what gets imported (make sure the project points to the right file in the build settings if you move it).

You should still keep in mind that using Swift and Objective-C within the same static library may have issues of its own. I suggest reading the Apple developer doc "Swift and Objective-C in the Same Project" on how to address importing Objective-C into Swift (using a bridging header) and how to use the Swift files in your Objective-C code (importing the generated -Swift.h for your library).


Swift doesn't support static library

Although the correct way should be create a framework, there is a workaround here.