Swift in native extensions

Many Apple APIs, such as StoreKit 2 or CryptoKit, are only available from Swift. A native extension can include its own Swift source files next to its Objective-C++ files. The build server compiles them together with the rest of the extension and links the result into the engine. This is separate from Swift packages and CocoaPods, which provide ready-made dependencies.

Swift code is compiled for macOS (arm64 and x86_64) and for iOS (devices and the simulator). Swift files are ignored when building for other platforms.

Adding Swift sources

Put the .swift files in the src folder of the extension, next to the C++ and Objective-C++ files:

myextension/
├── ext.manifest
└── src/
    ├── myextension.mm
    └── Greeter.swift

Keep all Swift files in src. A macOS or iOS build fails if it finds a Swift file in commonsrc or pluginsrc.

All Swift files of an extension are compiled together as one Swift module. The name of the module is the name from ext.manifest, where every character other than a letter, a digit or an underscore is replaced with _ (My-Extension becomes My_Extension). The Swift language mode is Swift 5.

The name of the module must differ from the modules that the code imports. An extension named StoreKit can’t import StoreKit, as the compiler takes the import for the module itself.

Calling Swift from Objective-C++

The entry point of an extension is still a C++ function registered with DM_DECLARE_EXTENSION, so an extension needs at least one .cpp or .mm file. Swift is called from this file through the generated -Swift.h header.

Only classes, protocols and methods marked with @objc are visible from Objective-C. Mark the classes public and derive them from NSObject:

src/Greeter.swift:

import Foundation

@objc(MyExtensionGreeter)
public class Greeter: NSObject {
    @objc public func message() -> String {
        return "Hello from Swift"
    }
}

src/myextension.mm:

#include <dmsdk/sdk.h>
#import <Foundation/Foundation.h>
#import "MyExtension-Swift.h"

static dmExtension::Result AppInitializeMyExtension(dmExtension::AppParams* params)
{
    MyExtensionGreeter* greeter = [[MyExtensionGreeter alloc] init];
    dmLogInfo("%s", [[greeter message] UTF8String]);
    return dmExtension::RESULT_OK;
}

DM_DECLARE_EXTENSION(MyExtension, "MyExtension", AppInitializeMyExtension, 0, 0, 0, 0, 0)

Calling Objective-C from Swift

To use Objective-C or C declarations from Swift, add a bridging header named -Bridging-Header.h to the src folder, for example src/MyExtension-Bridging-Header.h:

#import "mynative.h"

If the header exists, it is imported into the Swift module automatically. Headers it includes are searched in the include folder of the extension and in the folders listed in additionalSwiftIncludePaths (see below).

Apple frameworks, such as Foundation, UIKit or StoreKit, are imported from Swift with a normal import. The frameworks listed in frameworks and weakFrameworks of the ext.manifest are linked as usual. Most system frameworks are linked automatically when the Swift code imports them.

Using Swift packages from Swift

The products of the Swift packages that the extension declares in SwiftPackages.json are available to its Swift code. Import the module in a Swift file:

import Foundation
import Sentry

@objc(MyExtensionCrashReporter)
public class CrashReporter: NSObject {
    @objc public func start(dsn: String) {
        SentrySDK.start { options in
            options.dsn = dsn
        }
    }
}

The Objective-C headers of a package can be imported in the bridging header, in the same way as from an .mm file:

src/MyExtension-Bridging-Header.h:

#import <Sentry/Sentry.h>

Declare the package in the SwiftPackages.json of the extension that uses it. The build server builds the packages first and then the Swift code of the extension. If a package needs a newer OS than the minimum version of the engine, set it as minVersion in the SwiftPackages.json, and the Swift code of the extension is built for that version too. Using CocoaPods dependencies from Swift code is not supported. Use them from the Objective-C++ file and pass what is needed to the Swift code.

Compiler options

Options for the Swift compiler are set in the ext.manifest file, in the same context as the options for the C++ compiler:

name: MyExtension

platforms:
    ios:
        context:
            defines: ["MY_DEFINE"]
            swiftFlags: ["-strict-concurrency=complete"]
            additionalSwiftIncludePaths: ["include/vendor/headers"]
            additionalFrameworkSearchPaths: ["lib/ios/vendor/frameworks"]
    osx:
        context:
            defines: ["MY_DEFINE"]

The paths are relative to the extension folder, must stay inside of it, and may only contain letters, digits and ._+-/. Only the include, src, commonsrc, pluginsrc, lib/, manifests/ and res/ folders of an extension are sent to the build server, so the paths must point into one of them. Files in other folders, such as a vendor folder at the top of the extension, are not available during the build.

The Swift keys of the ios platform apply to both devices and the simulator. A build server that predates Swift support rejects them with a Manifest context variable unsupported error, and the project then fails to build until the Defold version and the build server are updated.

Minimum OS versions

The Swift runtime is part of every iOS and macOS version that Defold supports. The Swift code is built for the minimum iOS or macOS version of the build, and the application uses the Swift runtime of the operating system. Use an #available check before calling APIs that are newer than the minimum version.

A few parts of the runtime are part of the operating system only from a later version. For example, MutableSpan and related types are part of iOS 26 and macOS 26. If the minimum version is older and your Swift code or a dynamic framework of a package uses such a feature, the application may need an extra library to run on those devices. You don’t have to do anything for this: the build server finds the libraries that the engine and the dynamic libraries embedded with it use, as Xcode does, and adds them to the application. On iOS they are placed in the Frameworks folder of the application and are signed together with the rest of its code.

The minimum version of a build is the one that the engine supports, which is set by the Defold version and the build server. A minVersion in a SwiftPackages.json raises it for the packages, the Swift code and the link of the engine, as described in the Swift Package Manager manual. It can’t be lowered.

Limitations

Troubleshooting

no such module 'Name'
The module is not a product of a package declared in a SwiftPackages.json. Check that products lists a product that provides the module, as described in the Swift Package Manager manual.
Swift sources are only supported in the 'src' folder
Move the .swift files into the src folder of the extension.
'MyExtension-Swift.h' file not found
The module name is built from name in ext.manifest. Check the name in the #import line.
unknown type name 'MyExtensionGreeter'
The class is missing from the generated header. Check that it is marked public and @objc, derives from NSObject, and that the .mm file uses the name from @objc(...).
cannot find 'AppStore' in scope together with file 'A.swift' is part of module 'StoreKit'; ignoring import
The extension has the same name as a module it imports. Rename the extension in ext.manifest.
Undefined symbols ... _MyExtension
The extension has no DM_DECLARE_EXTENSION. Add a .mm file that registers the extension as described above.
Invalid flag - 'swiftFlags'
The flag is not on the allow list. Remove it or use one of the listed flags.
invalid 'swiftFlags' value
The value has whitespace, a backslash or template syntax, or names a response file. Put each flag and each value in its own list item.