...
1Use in Swift {#flatbuffers_guide_use_swift}
2=========
3
4## Before you get started
5
6Before diving into the FlatBuffers usage in Swift, it should be noted that
7the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide
8to general FlatBuffers usage in all of the supported languages (including Swift).
9This page is designed to cover the nuances of FlatBuffers usage, specific to
10Swift.
11
12You should also have read the [Building](@ref flatbuffers_guide_building)
13documentation to build `flatc` and should be familiar with
14[Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
15[Writing a schema](@ref flatbuffers_guide_writing_schema).
16
17## FlatBuffers Swift library code location
18
19The code for the FlatBuffers Swift library can be found at
20`flatbuffers/swift`. You can browse the library code on the [FlatBuffers
21GitHub page](https://github.com/google/flatbuffers/tree/master/swift).
22
23## Testing the FlatBuffers Swift library
24
25The code to test the Swift library can be found at `flatbuffers/tests/swift/tests`.
26The test code itself is located in [flatbuffers/tests/swift/tests](https://github.com/google/flatbuffers/blob/master/tests/swift/tests).
27
28To run the tests, use the [SwiftTest.sh](https://github.com/google/flatbuffers/blob/master/tests/swift/tests/SwiftTest.sh) shell script.
29
30*Note: The shell script requires [Swift](https://swift.org) to
31be installed.*
32
33## Using the FlatBuffers Swift library
34
35*Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
36example of how to use FlatBuffers in Swift.*
37
38FlatBuffers supports reading and writing binary FlatBuffers in Swift.
39
40To use FlatBuffers in your own code, first generate Swift structs from your
41schema with the `--swift` option to `flatc`. Then include FlatBuffers using `SPM` in
42by adding the path to `FlatBuffers/swift` into it. The generated code should also be
43added to xcode or the path of the package you will be using. Note: sometimes xcode cant
44and wont see the generated files, so it's better that you copy them to xcode.
45
46For example, here is how you would read a FlatBuffer binary file in Swift: First,
47include the library and copy thegenerated code. Then read a FlatBuffer binary file or
48a data object from the server, which you can pass into the `GetRootAsMonster` function.
49
50~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
51 import FlatBuffers
52
53 typealias Monster1 = MyGame.Sample.Monster
54 typealias Vec3 = MyGame.Sample.Vec3
55
56 let path = FileManager.default.currentDirectoryPath
57 let url = URL(fileURLWithPath: path, isDirectory: true).appendingPathComponent("monsterdata_test").appendingPathExtension("mon")
58 guard let data = try? Data(contentsOf: url) else { return }
59
60 let monster = Monster.getRootAsMonster(bb: ByteBuffer(data: data))
61~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62
63Now you can access values like this:
64
65~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
66 let hp = monster.hp
67 let pos = monster.pos // uses native swift structs
68 let pos = monster.mutablePos // uses flatbuffers structs
69~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70
71
72In some cases it's necessary to modify values in an existing FlatBuffer in place (without creating a copy). For this reason, scalar fields of a Flatbuffer table or struct can be mutated.
73
74~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
75 var byteBuffer = ByteBuffer(bytes: data)
76 // Get an accessor to the root object inside the buffer.
77 let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
78 // let monster: Monster = getRoot(byteBuffer: &byteBuffer)
79
80 if !monster.mutate(hp: 10) {
81 fatalError("couldn't mutate")
82 }
83 // mutate a struct field using flatbuffers struct
84 // DONT use monster.pos to mutate since swift copy on write
85 // will not mutate the value in the buffer
86 let vec = monster.mutablePos.mutate(z: 4)
87
88 // This mutation will fail because the mana field is not available in
89 // the buffer. It should be set when creating the buffer.
90 if !monster.mutate(mana: 20) {
91 fatalError("couldn't mutate")
92 }
93~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
95The term `mutate` is used instead of `set` to indicate that this is a special use case. All mutate functions return a boolean value which is false if the field we're trying to mutate is not available in the buffer.
96
97<br>
View as plain text