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.
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.
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)
Foundation. Types from other frameworks are only forward declared in it. Import the header of the framework (for example UIKit) in the .mm file before you call methods that use those types.throws functions are exposed as methods with an NSError ** parameter, and async functions as methods with a completion handler.@objc class..mm files are only compiled for iOS and macOS, so an extension with only .mm and .swift files fails on other platforms with Extension has no source!. To also build the extension for other platforms, put DM_DECLARE_EXTENSION in a .cpp file and call the .mm file from it inside a DM_PLATFORM_IOS or DM_PLATFORM_OSX guard.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.
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.
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"]
defines - The defines set for C++ are passed to Swift as conditional compilation names, so #if MY_DEFINE works in Swift. The value of a define, such as MY_DEFINE=1, is dropped, as Swift only supports names.swiftFlags - Additional flags for the Swift compiler. Each flag and each value is a separate list item. The flags are filtered by an allow list, and all other flags fail the build with an Invalid flag - 'swiftFlags' error. A value with whitespace, a backslash or template syntax, or one that names a response file, fails with an invalid 'swiftFlags' value error instead. The allowed flags are:
-enable-upcoming-feature - Enable an upcoming language feature, for example ["-enable-upcoming-feature", "ExistentialAny"].-strict-concurrency=minimal|targeted|complete-enforce-exclusivity=checked|unchecked-O, -Osize and -Onone - The optimization level. The default is -O.-suppress-warningsadditionalSwiftIncludePaths - Folders that the Swift compiler searches for headers and modules, in addition to the include folder of the extension.additionalFrameworkSearchPaths - Folders that are searched for frameworks, in addition to the lib/ folder of the extension. This is not specific to Swift: the folders are used by the Swift compiler, by the C++ and Objective-C++ compiler for the sources of the extension, and by the linker.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.
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.
DM_DECLARE_EXTENSION. Swift code cannot register the extension, and the C++ API of the Defold SDK (dmsdk) is not available from Swift, so call it from the Objective-C++ file. The dmsdk headers that are C-compatible, such as dmsdk/dlib/hash.h, can be included in the bridging header. A build with only .swift files fails to link with an undefined symbol error named after the extension.Swift sources are not supported by this Defold SDK version error.library artifact (--build-artifacts library in Bob), so an Objective-C++ file that imports -Swift.h fails to compile there.no such module 'Name'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.swift files into the src folder of the extension.'MyExtension-Swift.h' file not foundname in ext.manifest. Check the name in the #import line.unknown type name 'MyExtensionGreeter'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 importext.manifest.Undefined symbols ... _MyExtensionDM_DECLARE_EXTENSION. Add a .mm file that registers the extension as described above.Invalid flag - 'swiftFlags'invalid 'swiftFlags' value