Swift
    • PDF

    Swift

    • PDF

    Article Summary

    Available in Classic and VPC

    This document describes how to create and use Swift actions in a variety of ways, and includes application examples.

    Create actions

    The process of creating Swift actions is similar to creating JavaScript actions. A code written in Swift may contain multiple functions, but the main function must be declared as the starting point of a program. With this taken into account, the following is the simple example code "hello" in Swift, printing "Hello World in Naver!," along with a 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 code written above.

    compute-15-2-401.png

    Create actions by packaging with executable Swift action

    When creating an action using Swift source code files in Cloud Functions, the action must be compiled as binary before being executed. Once compiled, it will respond quickly to follow-up action calls occurring until the container on hold for tasks is eliminated.
    After the container is removed, there's delay in response when an action is run for the first time. Such a delay in response is known as a cold-start delay. To avoid such cold-start delays, an action must be created by compiling the Swift file to a binary directly, and uploading the ZIP file created.
    The easiest way to create a binary is to build it in the same environment as the environment for it to be executed. The following is a sequence for creating an action by directly creating a binary through a simple example.

    1. Run a Swift action container using Docker.

      $ docker run --rm -it -v "$(pwd):/owexec" openwhisk/action-swift-v3.1.1 bash
      
      • The bash shell within the Docker container can be used through this command.
    2. Copy the source code and prepare the 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
      
    3. Create the Package.swift file and add dependency packages if necessary.

      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 the example-package-deckofplayingcards dependency package.
      • CCurl, Kitura-net, and SwiftyJSON are included in the default Swift action, so add them to Package.swift.
    4. Copy Package.swift to the spm-build folder.

      $ cp /owexec/Package.swift /swift3Action/spm-build/Package.swift
      
    5. Go to the spm-build folder and compile.

      $ cd /swift3Action/spm-build
      $ swift build -c release
      
    6. Create the ZIP file, and exit the Docker container.

      $ zip /owexec/hello.zip .build/release/Action
      $ exit
      
      • The hello.zip file is created in the same folder as hello.swift.
    7. Upload the ZIP file created, and create a new action under the name helloSwifty.

      • After running the action, you can see the speed is quicker.

    Was this article helpful?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.