---
title: "Interfaces"
url: https://docs.zeroc.com/ice/3.8/slice/interfaces
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).

# Interfaces

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

## Syntax and Semantics of Interfaces

A Slice interface defines the operations that clients invoke on an Ice object. For example:

```slice
module M
{
    struct TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }

    interface Clock
    {
        TimeOfDay getTime();
        void setTime(TimeOfDay time);
    }
}
```

`Clock` defines two operations: `getTime` returns the current time, and `setTime` sets it. A client calls these operations on a proxy for an Ice object that implements `Clock`.

An interface contains only operations: you cannot define a type, an exception, or a field inside an interface.

An interface can be empty:

```slice
interface Empty {}
```

## Client-Side Mapping for Interfaces

### Proxy Protocols

On the client side, a Slice interface maps to an empty Swift protocol. A public extension of this protocol provides an async method for each Slice operation of your Slice interface.

Consider the following Slice interface:

```slice
module M
{
    interface Simple
    {
        void op();
    }
}
```

The Slice compiler generates the following definitions for use by the client:

```swift
// in module M

public protocol SimplePrx: Ice.ObjectPrx {}

public extension SimplePrx {
    func op(context: Ice.Context? = nil) async throws {
        ...
    }
}
```

As you can see, the compiler generates a *proxy protocol* `SimplePrx`. In general, the generated name is `<interface-name>Prx`.

In the client, an instance of `SimplePrx` is a proxy for an Ice object that implements `Simple`: it holds the addressing information of this object, such as its identity and endpoints; see [Proxy Types](https://docs.zeroc.com/ice/3.8/slice/interfaces/proxy-types.md).

### Creating a Proxy

For each proxy, the Slice compiler generates a `makeProxy` factory function in the same Swift module. With our previous example:

```swift
public protocol SimplePrx: Ice.ObjectPrx {}

public func makeProxy(communicator: Ice.Communicator,
                      proxyString: String,
                      type: SimplePrx.Protocol) throws -> SimplePrx {
    ...
}
```

Call `makeProxy` to create a proxy from a communicator and a “stringified” proxy:

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

### Inheritance from `Ice.ObjectPrx`

All generated proxy protocols inherit directly or indirectly from the `Ice.ObjectPrx` protocol, reflecting the fact that all Slice interfaces implicitly inherit from `Object`.

### Interface Inheritance

Inheritance relationships among Slice interfaces are maintained in the generated Swift protocols. For example:

```slice
module M
{
    interface A { ... }
    interface B { ... }
    interface C extends A, B { ... }
}
```

The generated code for `CPrx` reflects the inheritance hierarchy:

```swift
public protocol CPrx: APrx, BPrx {}
```

Given a proxy for `C`, a client can invoke any operation defined for interface `C`, as well as any operation inherited from `C`'s base interfaces.

### Creating a Proxy of Another Type

For each proxy, the Slice compiler generates two helper functions that allow you to convert any proxy into a proxy of this type. With our Simple example:

```swift
public protocol SimplePrx: Ice.ObjectPrx {}

public func uncheckedCast(prx: Ice.ObjectPrx,
                          type: SimplePrx.Protocol,
                          facet: String? = nil) -> SimplePrx {
    ...
}

public func checkedCast(prx: Ice.ObjectPrx,
                        type: SimplePrx.Protocol,
                        facet: String? = nil,
                        context: Ice.Context? = nil) async throws -> SimplePrx? {
    ...
}
```

#### uncheckedCast

The `uncheckedCast` function allows you to convert a proxy into another proxy. For example:

```swift
// Convert a SimplePrx into a WidgetPrx, even though the two types are unrelated.
let widget = uncheckedCast(prx: simple, type: WidgetPrx.self)
```

`uncheckedCast` is a local operation that always succeeds.

#### checkedCast

`checkedCast` makes a remote call to the target object to check whether it implements the Slice interface of the requested proxy type. For example:

```swift
// Call operation ice_isA on the Ice object to check if it implements Slice interface
// Widget.
let widget = try await checkedCast(prx: simple, type: WidgetPrx.self)
```

If the target object implements the Slice interface, `checkedCast` returns a non-nil proxy, just like `uncheckedCast`. If the target object doesn’t implement this interface, `checkedCast` returns nil. `checkedCast` can also throw an exception, for example if it cannot reach the remote object.

Use `uncheckedCast` when you know the target object implements the Slice interface, and `checkedCast` when you need to ask the target object whether it implements this interface. You rarely need either: you usually [create a proxy](https://docs.zeroc.com/ice/3.8/runtime/invocation/creating-proxies/swift.md#creating-a-proxy-of-another-type) of the type you need directly.

### Proxy Factory Methods

The base proxy interface `ObjectPrx` supports a variety of methods for customizing a proxy. Since proxies are immutable, each of these factory methods returns a copy of the original proxy that contains the desired modification. For example, you can obtain a proxy configured with a ten second invocation timeout as shown below:

```swift
var greeter = try makeProxy(communicator: ..., proxyString: ..., type: GreeterPrx.self)

// Create a new GreeterPrx and assign it to greeter.
greeter = greeter.ice_invocationTimeout(10000)
```

The factory methods usually return a proxy of the same type as the current proxy, as in the example above.

The only exceptions are the factory methods `ice_facet` and `ice_identity`. Calls to either of these methods may produce a proxy for an object of an unrelated type, and you need to convert the returned proxy into a proxy of that type. For example:

```swift
let greeter = try makeProxy(communicator: ..., proxyString: ..., type: GreeterPrx.self)
let greeterAdmin = uncheckedCast(
    prx: greeter.ice_facet("admin"), type: GreeterAdminPrx.self)
```

## Server-Side Mapping for Interfaces

The server-side mapping for interfaces provides an up-call API for the Ice runtime: by implementing instance methods in a servant class, you provide the hook that gets the thread of control from the Ice server-side runtime into your application code.

### Skeleton Protocols

On the server side, interfaces map to *skeleton* protocols. A skeleton protocol specifies an instance method for each operation on the corresponding Slice interface. For example, consider our Slice definition for the `Node` interface:

```slice
module Filesystem
{
    interface Node
    {
        idempotent string name();
    }
    // ...
}
```

The Slice compiler generates the following definitions for this interface:

```swift
// Skeleton protocol
public protocol Node: Ice.Dispatcher {
    func name(current: Ice.Current) async throws -> Swift.String
}

extension Node {
    public func dispatch(
        _ request: sending Ice.IncomingRequest) async throws ->
            Ice.OutgoingResponse {
        ...
    }
}
```

The important points to note here are:

- For each Slice interface `<interface-name>`, the compiler generates a Swift protocol `<interface-name>` (`Node` in this example).
- This skeleton protocol extends `Ice.Dispatcher`, and the Slice compiler generates an extension for the skeleton protocol that implements `dispatch` (Dispatcher’s only method).
- The base servant protocol in Swift is `Ice.Dispatcher`; skeleton protocols do not derive from `Ice.Object`.

### Servant Implementation

In order to provide an implementation for an Ice object, you must create a servant struct, class or actor that adopts the corresponding skeleton protocol. For example, to create a servant for the `Node` interface, you could write:

```swift
struct MNode: Node {
    private let name: String

    init(name: String) {
        self.name = name
    }

    func name(current _: Ice.Current) -> String {
        name
    }
}
```

Note that `MNode` adopts `Node`, the skeleton protocol.

As far as Ice is concerned, the `MNode` struct must implement only a single method: the `name` method from its skeleton. This makes the servant struct a concrete type that can be instantiated. You can add other methods and fields as you see fit to support your implementation. For example, in the preceding definition, we added a `name` field and an initializer.

A skeleton protocol inherits `Sendable` from `Ice.Dispatcher`, and the object adapter dispatches each request in its own task, so several tasks can call the same servant concurrently. A struct servant such as `MNode` satisfies this requirement when all its stored properties are `Sendable`. Implement a servant with mutable state as an actor. An actor can't return a value whose type doesn't conform to `Sendable` from a skeleton method, so when the type of a return value or out parameter contains a class, implement the servant as a class that synchronizes access to its state and is declared `@unchecked Sendable`.

## See Also

- [Operations](https://docs.zeroc.com/ice/3.8/slice/operations/swift.md)
- [User Exceptions](https://docs.zeroc.com/ice/3.8/slice/exceptions/swift.md)
