On this page:
Basic Swift Mapping for Operations
As we saw in the mapping for interfaces, for each operation on a Slice interface, the corresponding proxy protocol extension contains a method with the same name. To invoke an operation, you call it via the proxy. For example, here is part of the definitions for our file system:
name operation returns a value of type
string. Given a proxy to an object of type
Node, the client can invoke the operation as follows:
idempotent Operations in Swift
You can add an
idempotent qualifier to a Slice operation. As far as the signature for the corresponding proxy method is concerned,
idempotent has no effect. For example, consider the following interface:
The proxy interface for this is:
idempotent affects an aspect of the operation invocation, not interface, it makes sense for the two operations to be mapped the same.
Passing Parameters in Swift
The parameter passing rules for the Swift mapping are very simple: each
in parameter of a Slice operation is mapped to a corresponding parameter in the mapped Swift method.
Here is an interface with operations that pass parameters of various types from client to server:
The Slice compiler generates the following proxy for these definitions:
Given a proxy to a
ClientToServer interface, the client code can pass parameters as in the following example:
When an operation has a single input parameter the proxy generated method doesn't use a label for the input parameter, for methods that have more than one input parameter each input parameter uses a label that corresponds to the name of the parameter in the Slice definition.
The mapping for an operation depends on how many values it returns, including out parameters and a non-
void return value:
- Zero values
The corresponding Swift method returns
void. For the purposes of this discussion, we're not interested in these operations.
The corresponding Swift method returns the mapped type, regardless of whether the Slice definition of the operation declared it as a return value or as an out parameter. Consider this example:
The mapping generates corresponding methods with identical signatures:
- Multiple values
The mapped Swift method returns a tuple. If the operation declares a return value, this value is provided as the first element of the tuple with the label
returnValue. If an out parameter is also named
returnValue, the label for the operation's return value is named
Here are the same Slice definitions we saw earlier, but this time with all parameters being passed in the
out direction, along with one additional operation to better demonstrate the mapping:
The Slice compiler generates the following code for these definitions:
An optional parameter is mapped to a Swift parameter with the corresponding Swift optional type. Consider the following operation:
The mapping for this operation is shown below:
The generated code uses nil default value for optional parameters, omitting a default parameter will result in a unset optional.
The Swift mapping does not allow you to represent an optional proxy or class parameter that holds a
null value. Consider this example:
The Ice encoding allows
null values for class instances so with some language mappings you can pass
addRequired and the server will receive it as
null. There is however no way to do this with the Swift mapping: passing
addOptional means passing an optional that is not set.
Exception Handling in Swift
Slice exceptions are thrown as Swift exceptions, so you can simply enclose one or more operation invocations in a do-
Typically, you will catch only a few exceptions of specific interest around an operation invocation; other exceptions, such as unexpected run-time errors, will typically be handled by exception handlers higher in the hierarchy. For example: