Swift Mapping for Structures
A Slice structure maps to a Swift structure when this Slice structure does not have (recursively) any Slice class data member. Conversely, a Slice structure maps to a Swift class when this Slice structure has (recursively) one or more Slice class data members.
On this page:
Mapping to Swift Structure
Consider the following Slice structure:
struct Point { double x; double y; }
This simple structure does not have any Slice class member so it maps to a public Swift structure:
public struct Point { 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 data member in the Slice definition, the Swift structure contains a corresponding public stored property of the same name.
The generated structure provides default and memberwise initializers marked public
. The memberwise initializer allows you to construct and initialize a structure in a single statement:
let p = Point(x: 5.1, y: 7.8)
When all the stored properties of the generated Swift structure are Hashable, the generated structure is itself hashable. For example:
struct TimeOfDay { short hour; short minute; short second; }
The corresponding Swift structure adopts Hashable
:
public struct TimeOfDay: Hashable { 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 data member of a class type is mapped to a Swift class. Take the Entry structure below:
class Data { ... } struct Entry { int key; Data value; }
Entry
is mapped to a public Swift class:
public 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 data member in the Slice definition, the Swift structure contains a corresponding public stored property of the same name. Data members of type class or proxy are mapped to Swift optionals: the mapped type for value
in the example above is Data?
.
In addition to the stored properties, the generated class provides default and memberwise initializers marked public
.
A Slice structure is mapped to a Swift class when this Slice structure contains a class data member anywhere: it can be a direct member or a nested member such as:
class Data { ... } struct Entry { int key; Data value; } sequence<Entry> EntryList; // Mapped to a Swift class, since entries contains indirectly a class member struct Record { string name; EntryList entries; }
Mapping for Data Members Default Values
The generated Swift code provides default values of 0, nil or empty for every stored properties of your Swift class or structure. If you wish to use another default value for a mapped property, you can declare this default value in your Slice definition, and the generated Swift code will use this default value instead.