---
title: "Creating Proxies"
url: https://docs.zeroc.com/ice/3.8/runtime/invocation/creating-proxies
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).

# Creating Proxies

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

This page describes all the ways an application can create a proxy.

## Creating a Proxy from a String

The Slice compiler generates a `makeProxy` function that allows you to construct a proxy from a communicator and a [stringified representation](https://docs.zeroc.com/ice/3.8/runtime/invocation/syntax-for-stringified-proxies.md) of the proxy, as shown in the following example:

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

## Creating a Proxy from a Property

Rather than hard-coding a stringified proxy as the previous example demonstrated, an application can gain more flexibility by externalizing the proxy in a configuration property. For example, we can define a property that contains our stringified proxy as follows:

```config
Greeter.Proxy=greeter:tcp -h localhost -p 4061
```

We can use the `propertyToProxy` method on `Communicator` to convert the property's value into a proxy. A null proxy (nil) is returned if no property is found with the specified name.

```swift
guard let proxy = try communicator.propertyToProxy("Greeter.Proxy") else {
    fatalError("the Greeter.Proxy property is not set")
}
let greeter = uncheckedCast(prx: proxy, type: GreeterPrx.self)
```

`propertyToProxy` returns an `ObjectPrx`. Call [`uncheckedCast`](https://docs.zeroc.com/ice/3.8/slice/interfaces/swift.md#uncheckedcast) on this base proxy to create a `GreeterPrx`.

As an added convenience, `propertyToProxy` allows you to define subordinate properties that configure the proxy's local settings. The properties below demonstrate this feature:

```config
Greeter.Proxy=greeter:tcp -h localhost -p 4061
Greeter.Proxy.EndpointSelection=Ordered
```

These additional properties simplify the task of customizing a proxy (as you can with [proxy methods](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/objectprx)) without the need to change the application's code. The properties shown above are equivalent to the following statements:

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

greeter = greeter.ice_endpointSelection(.Ordered)
```

The [Proxy Properties](https://docs.zeroc.com/ice/3.8/property-reference/proxy-properties/swift.md) page lists the properties you can define this way.

Note that proxy properties can themselves have proxy properties. For example, the following sets the `EndpointSelection` property on the default locator's router:

```config
Ice.Default.Locator.Router.EndpointSelection=Ordered
```

## Creating a Proxy from an Object Adapter

An object adapter creates the proxies that clients use to call operations on the Ice objects it hosts. `createProxy` returns a proxy with the given identity, and `add`, `addFacet`, `addWithUUID` and `addFacetWithUUID` register a servant with the [Active Servant Map](https://docs.zeroc.com/ice/3.8/runtime/dispatch/active-servant-map/swift.md) and return a proxy for the Ice object this servant implements. See [ObjectAdapter](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/objectadapter) in the API reference for all the methods that create proxies.

These proxies use the options set by the object adapter's [ProxyOptions](https://docs.zeroc.com/ice/3.8/property-reference/object-adapter-properties/swift.md) property, such as `-o` for oneway proxies.

## Creating a Proxy from a Connection

`createProxy` on a connection returns a fixed proxy: invocations on this proxy use only this connection. A server uses such a proxy to call back a client over a [bidirectional connection](https://docs.zeroc.com/ice/3.8/runtime/connection-management/bidirectional-connections/swift.md).

## Receiving a Proxy from an Operation

An application can also receive a proxy as the result of an Ice invocation. Consider the following Slice definitions:

```slice
interface Account { ... }
interface Bank
{
    Account* findAccount(string id);
}
```

Invoking the `findAccount` operation returns a proxy for an `Account` object.

For example:

```swift
let account = try await bank.findAccount("WXY-123456")
```

## Proxy Factory Methods

A proxy is immutable. Its factory methods, such as `ice_oneway`, `ice_facet` and `ice_invocationTimeout`, return a proxy with the requested setting, and leave the original proxy unchanged. See [ObjectPrx](https://code.zeroc.com/ice/3.8/api/swift/documentation/ice/objectprx) in the API reference for the complete list of factory methods and the accessors that return the current settings.

The [language mapping for interfaces](https://docs.zeroc.com/ice/3.8/slice/interfaces/swift.md) describes the type of the proxy that these factory methods return.

## Creating a Proxy of Another Type

`uncheckedCast` and `checkedCast` create a proxy of the desired type from an existing proxy, for the same Ice object. Despite their names, these functions are not casts: they return a new proxy and leave the original proxy unchanged. `uncheckedCast` creates the new proxy without contacting the target object. `checkedCast` first calls `ice_isA` on the target object to verify that it implements the requested interface, and returns a null proxy if it does not.

You rarely need these functions. You use them when you hold a proxy of a more general type than the interface you want to call, such as the proxy returned by an operation whose return type is `Object*`.

The [language mapping for interfaces](https://docs.zeroc.com/ice/3.8/slice/interfaces/swift.md) shows these functions.

## Local Settings

A proxy has two kinds of settings. Ice marshals the identity, facet, invocation mode, encoding version, and endpoints or adapter ID of a proxy when it sends the proxy to another application. The other settings, such as the router, locator, locator cache timeout, endpoint selection type, connection caching, invocation timeout, context and connection ID, are local, and Ice does not marshal them.

A proxy that Ice unmarshals starts with the default value of each local setting, whatever the value of this setting in the application that sent the proxy.

## Proxy Defaults

The [Ice.Default.\*](https://docs.zeroc.com/ice/3.8/property-reference/ice-default-properties/swift.md) properties set the initial settings of the proxies you create from a string or a property, of the proxies an object adapter creates, and of the proxies Ice unmarshals from the payload of a request or response. A proxy property overrides the corresponding default for the proxy that `propertyToProxy` creates.

For example, the following property sets the endpoint selection type of these proxies to `Ordered`:

```config
Ice.Default.EndpointSelection=Ordered
```

## See Also

- [Communicator](https://docs.zeroc.com/ice/3.8/runtime/communicator/swift.md)
- [Syntax for Stringified Proxies](https://docs.zeroc.com/ice/3.8/runtime/invocation/syntax-for-stringified-proxies.md)
- [Proxy Properties](https://docs.zeroc.com/ice/3.8/property-reference/proxy-properties/swift.md)
