swift_code_13.swift (785B)
1 import FlatBuffers 2 import Foundation 3 4 func run() { 5 // create a ByteBuffer(:) from an [UInt8] or Data() 6 let buf = [] // Get your data 7 var byteBuffer = ByteBuffer(bytes: buf) 8 // Get an accessor to the root object inside the buffer. 9 let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer) 10 // let monster: Monster = getRoot(byteBuffer: &byteBuffer) 11 12 let hp = monster.hp 13 let mana = monster.mana 14 let name = monster.name // returns an optional string 15 16 let pos = monster.pos 17 let x = pos.x 18 let y = pos.y 19 20 // Get and check if the monster has an equipped item 21 if monster.equippedType == .weapon { 22 let _weapon = monster.equipped(type: Weapon.self) 23 let name = _weapon.name // should return "Axe" 24 let dmg = _weapon.damage // should return 5 25 } 26 }