Building Ice Applications in Swift

This page provides important information for Swift developers.

On this page:

Prerequisites

In order to build applications with Ice in Swift, you need:

  1. The Slice to Swift compiler 
    slice2swift is a command-line tool written in C++. You need it only during development. For Ice 3.7 beta, you can install slice2swift with:

    brew install zeroc-ice/tap/slice2swift
  2. The Ice Framework for Swift

    You can install Carthage using Homebrew:

    brew install carthage

    Then add the Ice framework to your Cartfile:

    echo 'github "zeroc-ice/ice" "v3.7-beta2-swift"' >> Cartfile

    Finally tell Carthage to build and install your Cartfile frameworks and their dependencies:

    carthage update

    The following frameworks will be either built or downloaded:

    Minimum Requirements

    Ice.framework and PromiseKit.framework are the minimum requirements for using Ice for Swift.

    Add Ice and PromiseKit repositories to your package descriptor dependencies:

    dependencies: [
        .package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.10.0"),
        .package(url: "https://github.com/zeroc-ice/ice.git", from: "3.7.3-beta.2+spm")
    ],

    The following modules are available from Ice package:

    • Ice
    • IceGrid
    • IceStorm
    • Glacier2

    The PromiseKit package provides the PromiseKit module.


    Ice and PromiseKit modules are the minimum requirements for using Ice for Swift.

Compile Slice Files

This allows you to compile Slice files into Swift code each time you build your application. With this setup, you don't need to (and should't) put the code generated by slice2swift under source-control. 

On each target's Build Rules settings tab, click the + icon. Under Process (Source files with names matching:) enter the names of the Slice files you want to compile, typically: 

Process [Source files with names matching:]
*.ice

Add a Custom Script to run the slice2swift compiler:

Custom script:
if [ -f /usr/local/bin/slice2swift ]; then
	SLICE2SWIFT=/usr/local/bin/slice2swift
    SLICEDIR=/usr/local/share/slice2swift/slice
else
    echo "Failed to locate slice2swift compiler"
    exit 1
fi

"$SLICE2SWIFT" -I"$SLICEDIR" -I"$INPUT_FILE_DIR" --output-dir "$DERIVED_FILE_DIR" "$INPUT_FILE_PATH"

Add the path to the generated source file to the Output Files:

Output Files
$(DERIVED_FILE_DIR)/$(INPUT_FILE_BASE).swift

Swift Package Manager doesn't currently support custom build rules and you will have to manually compile your Slice definitions using the slice2swift compiler, and add the generated source files to your package sources directory.


Adding Ice Frameworks

 Xcode Application Targets

macOS

Drag the framework binaries from Carthage/Build/Mac into your application’s Xcode project. Ensure they are included in your targets' General / Embedded settings tab. 

iOS/tvOS/watchOS

Drag the framework binaries from Carthage/Build/<platform> into your application’s Xcode project. Ensure they are included in your targets' General / Linked Frameworks and Libraries settings tab. 

On each target's Build Phases settings tab, click the + icon and choose New Run Script Phase. Create a Run Script with the following contents:

Shell script
/usr/local/bin/carthage copy-frameworks

Add the paths to the frameworks you want to use under Input Files. For example:

Input Files
$(SRCROOT)/Carthage/Build/iOS/IceImpl.framework
$(SRCROOT)/Carthage/Build/iOS/Ice.framework
$(SRCROOT)/Carthage/Build/iOS/PromiseKit.framework

Add the paths to the copied frameworks to the Output Files. For example:

Output Files
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/IceImpl.framework
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/Ice.framework
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/PromiseKit.framework

Refer to the following links for additional information:

Xcode Command Line Tool Targets

Drag the framework binaries from Carthage/Build/Mac into your application’s Xcode project. Ensure they are included in your targets' General / Linked Framework and Libraries settings tab.

In the package descriptor add the Ice and PromiseKit frameworks as dependencies to your targets:

targets: [
    .target(name: "Client", dependencies: ["Ice", "PromiseKit"]),
    .target(name: "Server", dependencies: ["Ice", "PromiseKit"])
]

There are additional modules for Glacier2, IceStorm and IceGrid.


PromiseKit

The Ice API for asynchronous invocations and dispatches relies on promises provided by PromiseKit. As a result, you need to bundle the PromiseKit framework in addition to the Ice framework with your Ice-based applications.

Using the Sample Programs

Sample programs are available at the ice-demos GitHub repository. You can browse this repository to see build and usage instructions for all supported programming languages. You can clone this repository with:

git clone -b swift https://github.com/zeroc-ice/ice-demos.git
cd ice-demos