swift_code_7.swift (1017B)
1 import FlatBuffers 2 import Foundation 3 4 func run() { 5 // create a `FlatBufferBuilder`, which will be used to serialize objects 6 let builder = FlatBufferBuilder(initialSize: 1024) 7 8 let weapon1Name = builder.create(string: "Sword") 9 let weapon2Name = builder.create(string: "Axe") 10 11 // start creating the weapon by calling startWeapon 12 let weapon1Start = Weapon.startWeapon(&builder) 13 Weapon.add(name: weapon1Name, &builder) 14 Weapon.add(damage: 3, &builder) 15 // end the object by passing the start point for the weapon 1 16 let sword = Weapon.endWeapon(&builder, start: weapon1Start) 17 18 let weapon2Start = Weapon.startWeapon(&builder) 19 Weapon.add(name: weapon2Name, &builder) 20 Weapon.add(damage: 5, &builder) 21 let axe = Weapon.endWeapon(&builder, start: weapon2Start) 22 23 // Create a FlatBuffer `vector` that contains offsets to the sword and axe 24 // we created above. 25 let weaponsOffset = builder.createVector(ofOffsets: [sword, axe]) 26 27 // Name of the Monster. 28 let name = builder.create(string: "Orc") 29 }