Available in Classic and VPC
This section describes how to create and use swift actions in a variety of ways, and includes application examples.
Create action
Creating Swift actions is similar to creating JavaScript actions. While Swift code can contain multiple functions, a main function must be declared as the program's entry point. With this taken into account, the following is a simple example code hello in Swift, printing "Hello World in Naver!" including the name and location.
func main(args: [String:Any]) -> [String:Any] {
let name: String = args["name"] as? String ?? "World"
let place: String = args["place"] as? String ?? "Naver"
return [ "payload" : "Hello \(name) in \(place)!" ]
}
The following is the process of an action called "helloSwift" in the console, using the example code written above.

Package and create action as executable Swift action
When creating actions using Swift source code files in Cloud Functions, code must be compiled to binary before running. After initial compilation, subsequent action calls respond quickly until the waiting container is removed.
After container removal, response delays known as cold-start-delay occur on the first action run. To avoid cold-start delay, compile Swift files directly to binary and create actions by uploading the generated ZIP file.
The simplest way to generate binaries is to build in the same environment where they will run. To create an action using a generated binary, take the following steps:
-
Run Swift action container using Docker.
$ docker run --rm -it -v "$(pwd):/owexec" openwhisk/action-swift-v3.1.1 bash- You can use the bash shell in the Docker container with this command
-
Copy source code and prepare build.
$ cp /owexec/hello.swift /swift3Action/spm-build/main.swift$ cat /swift3Action/epilogue.swift >> /swift3Action/spm-build/main.swift$ echo '_run_main(mainFunction:main)' >> /swift3Action/spm-build/main.swift -
Create Package.swift file if needed and add dependencies.
import PackageDescription let package = Package( name: "Action", dependencies: [ .Package(url: "https://github.com/apple/example-package-deckofplayingcards.git", majorVersion: 3), .Package(url: "https://github.com/IBM-Swift/CCurl.git", "0.2.3"), .Package(url: "https://github.com/IBM-Swift/Kitura-net.git", "1.7.10"), .Package(url: "https://github.com/IBM-Swift/SwiftyJSON.git", "15.0.1"), .Package(url: "https://github.com/watson-developer-cloud/swift-sdk.git", "0.16.0") ] )- Add example-package-deckofplayingcards dependency
- CCurl, Kitura-net, SwiftyJSON are included in basic Swift actions, so add them to Package.swift
-
Copy Package.swift to spm-build folder.
$ cp /owexec/Package.swift /swift3Action/spm-build/Package.swift -
Go to spm-build folder and compile.
$ cd /swift3Action/spm-build $ swift build -c release -
Create zip file and exit Docker container.
$ zip /owexec/hello.zip .build/release/Action $ exit- hello.zip file is created in same folder as hello.swift.
-
Upload created zip file to create new action named helloSwifty.
- Notice improved speed after running action.