---
title: "Writing a Greeter Client"
url: https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client
language: swift
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Writing a Greeter Client

Programming language: Swift. Other languages: [C++](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/php.md), [Python](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-client/ruby.md).

This page presents a step-by-step guide to writing the client-side of our Swift Greeter application.

This client creates a proxy to a remote object that implements the `Greeter` interface and invokes the `greet` operation on this object.

You can find the complete source code for this example in the [ice-demos repository](https://github.com/zeroc-ice/ice-demos/tree/3.8/swift/Ice/Greeter).

## Compile Slice File with Slice Compiler

To write a Swift application with Ice, first configure SwiftPM to compile the Slice definitions. The Ice package contains a plugin for this purpose, `CompileSlice`, which can be added to the executableTarget in `Package.swift`.

```swift
.executableTarget(
    name: "Client",
    dependencies: [.product(name: "Ice", package: "ice")],
    plugins: [.plugin(name: "CompileSlice", package: "ice")]
),
```

The `CompileSlice` plugin compiles the `.ice` files in the target's sources, and the files listed in a `slice-plugin.json` file in the target's sources. In this example, `Greeter.ice` is in the `slice` directory at the root of the package, outside `Sources/Client`, so `Sources/Client/slice-plugin.json` lists it, with a path relative to the directory that contains `slice-plugin.json`:

```json
{
  "sources": ["../../slice/Greeter.ice"]
}
```

The plugin compiles `Greeter.ice` into `Greeter.swift` and adds it as a source file of the Client target. The generated code provides the APIs that we’ll call in our client code, so it’s an essential step of the development process.

## Client Implementation

To get started we first need to import a few dependencies.

```swift
import Foundation
import Ice
```

- `Foundation`: Used to obtain the username.
- `Ice`: For accessing the Ice runtime

Next is the client logic, which can be broken down into four pieces:

### 1. Create a Communicator

First, we create a [Communicator](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/communicator) with `Ice.initialize`:

```swift
var args = CommandLine.arguments
let communicator = try Ice.initialize(&args)

defer {
    communicator.destroy()
}
```

The communicator is the main entry point into the Ice runtime. Its responsibilities include establishing connections to servers, caching these connections, and managing configuration properties. We also need a communicator to create a proxy (see next step).

Our client, like most Ice applications, creates a single communicator.

It is important to properly clean up the communicator when done, which we do with the defer block that calls `destroy()`.

### 2. Create a Greeter Proxy

Next, we need a way to call on a remote `Greeter` object. In Ice, this is done with *proxies*. Proxies are local constructs that represent remote Ice objects and provide methods to call operations on those objects.

We create a `Greeter` proxy with the `makeProxy` function that the Slice compiler generates for the `GreeterPrx` protocol:

```swift
let greeter = try makeProxy(
    communicator: communicator, proxyString: "greeter:tcp -h localhost -p 4061",
    type: GreeterPrx.self)
```

The `makeProxy` function accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says that the target Ice object is named “greeter” and can be reached via `tcp` on `localhost` on port `4061`.

> **Note**
>
> The name of the interface (`Greeter`) and the identity of the Ice object (`greeter`) are independent. The Ice objects hosted in the server could just as easily have identities like `santa`, `bugsBunny`, etc.

### 3. Make an Invocation

The third step is to call `greet` on the remote Ice object using our proxy and to print the greeting:

```swift
let greeting = try await greeter.greet(NSUserName())
print(greeting)
```

The `greet` method does all the heavy lifting for us: the proxy creates a request with the username, the communicator establishes a connection to `localhost:4061`, and the request is sent over it. When a response is received, the proxy unmarshals its payload and returns a string (the greeting).

Using `async`/`await` for this invocation offers several advantages:

- The calling thread can continue doing other work while `greet` waits for I/O.
- The `await` keyword signals to the reader that `greet` is a remote operation and may take time to complete.

### 4. Cleanup

When the main function exits, the defer statement destroys the communicator.

## Running the Client

To run the client, execute the following command (the executable will be compiled if necessary):

```shell
swift run Client
```

> **Note**
>
> This client won’t work unless you’ve also launched a [Greeter server](https://docs.zeroc.com/ice/3.8/greeter-example/writing-a-greeter-server/swift.md).
