---
title: "Structures"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures
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).

# Structures

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

## Struct Syntax

A structure groups one or more named fields, of any type, into a new type. For example:

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

This definition introduces a new type named `TimeOfDay`. The name of a field must be unique within its structure.

A structure contains only fields: as elsewhere in Slice, type definitions cannot be nested, except for [modules](https://docs.zeroc.com/ice/3.8/slice/modules/swift.md). Like every other type, you define a structure in a module.

## Language Mapping

A Slice structure maps to a Swift structure when this Slice structure does not have (recursively) any Slice class field. Conversely, a Slice structure maps to a Swift class when this Slice structure has (recursively) one or more Slice class field.

### Mapping to Swift Struct

Consider the following Slice structure:

```slice
struct Point
{
    double x;
    double y;
}
```

This simple structure does not have any Slice class field so it maps to a public Swift structure:

```swift
public struct Point: Sendable {
    public var x: Double = 0
    public var y: Double = 0

    public init() {}

    public init(x: Double, y: Double) {
        self.x = x
        self.y = y
    }
}
```

For each field in the Slice definition, the Swift structure contains a corresponding public stored property of the same name.

The generated Swift structure conforms to `Sendable`. When the Slice structure is a legal [dictionary key type](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/swift.md), the generated structure also conforms to `Hashable`. For example:

```slice
struct TimeOfDay
{
    short hour;
    short minute;
    short second;
}
```

The corresponding Swift structure conforms to `Hashable`:

```swift
public struct TimeOfDay: Hashable, Sendable {
    public var hour: Int16 = 0
    public var minute: Int16 = 0
    public var second: Int16 = 0

    public init() {}

    public init(hour: Int16, minute: Int16, second: Int16) {
        self.hour = hour
        self.minute = minute
        self.second = second
    }
}
```

### Mapping to Swift Class

A Slice structure with a field of a class type is mapped to a Swift class. Take the Entry structure below:

```slice
class Data
{
    ...
}

struct Entry
{
    int key;
    Data value;
}
```

`slice2swift` maps `Entry` to a public final Swift class:

```swift
public final class Entry {
    public var key: Int32 = 0
    public var value: Data? = nil

    public init() {}

    public init(key: Int32, value: Data?) {
        self.key = key
        self.value = value
    }
}
```

For each field in the Slice definition, the Swift structure contains a corresponding public stored property of the same name. Fields with type class or proxy are mapped to Swift optionals: the mapped type for `value` in the example above is `Data?`.

> **Tip**
>
> A Slice structure is mapped to a Swift class when this Slice structure contains a class field anywhere: it can be a direct field or a nested field such as:
>
> ```slice
> class Data
> {
>     ...
> }
>
> struct Entry
> {
>     int key;
>     Data value;
> }
>
> sequence<Entry> EntryList;
>
> // Mapped to a Swift class, since entries contains indirectly a class field.
> struct Record
> {
>     string name;
>     EntryList entries;
> }
> ```

### Generated Initializers

The mapped Swift struct or class has always two public initializers:

- a memberwise initializer that initializes all properties explicitly
- a parameterless initializer that assigns default values to all properties (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/swift.md))

## See Also

- [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/swift.md)
- [Classes](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/swift.md)
