How to Import Modules Without an Xcode Project in Swift

Swift without Xcode importing class

If you are on Linux, you can use the Swift Package Manager. It will allow you to import custom modules and to download external frameworks (aka dependencies). I believe however, you may need to build swift from source in order to use it.

The structure of your project will most likely need to change. SwiftPM is pretty strict about it.

With the typical setup of a package, you will need to have two sub folders within the Sources directory, one forming a library and the other forming an executable. For example,

ExampleProject/
Package.swift
Sources/
MyLibrary/
components.swift
MyExecutable/
main.swift

This structure will allow you to import module MyLibrary from the main.swift file.

Note that the only difference here is a library doesn't contain a main.swift file, while the executable does.

You will also need to make a manifest file - Package.swift - with SwiftPM also requires for building packages. Here are some of their examples on how to build packages.

How to import module into xcode

When you clone or or download the socket io project you get a directory with multiple subdirectories. One of the subdirectories is named Source. Simply drag that folder into Xcode (into the Project Navigator in the left sidebar).

You will then be presented with a screen like this:
Xcode - add to target

Make sure the box for your app is ticked in the "Add to targets" list. This is what the instructions mean by "Make sure you add the files to your target(s)"

Import my custom module/framework Xcode Swift

Drag your module's x-code project into the left pane with all the files.

Then click on your main project file and go to general, scroll down to Embedded Binaries and add it through the menu there.

Sample Image

getting module not found after importing packages in swift

Author of EPUBKit here, thanks for taking interest in my library!

The issue is reproducible in a new project which kinda makes sense but don't at the same time. I am glad Xcode is not adding a package to every single target in my project but at the same time why does it assume that I want it in the iOS target? Anyway, I know that this question was already answered in the comment and the problem is solved but I might as well just drop more detailed instructions. Maybe it will help someone.

1. Select Project from Project Navigator

Select te target that is not working for you, in this case it's the macOS target. In the General tab you will see the Frameworks, Libraries, and Embedded Content section. If the package is missing from the list, tap the plus button at the bottom.

Project without a package

2. Add Missing Library

Look for the one you are missing, in this case its of course EPUBKit and tap the Add button.

Choose frameworks and libraries to add

3. Success

Now the library is successfully added to your target. Go ahead and build the project to run and enjoy your app :)

Sample Image

Can't use cocoa pod modules once installed into Xcode project

Please dont name your Project copied of the framework you installed. Or try Cmd + Shift + K (Clean build), or just Build it first (Cmd + B)



Related Topics



Leave a reply



Submit