damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

create_your_first_buffer.tutorial (3888B)


      1 @Tutorial(time: 5) {
      2   @Intro(title: "After having our code generated") {
      3     After generating the code from the previous section, we will know start creating our monster object.
      4     We will create a monster object called orc.
      5     }
      6     
      7     @Section(title: "Building your first buffer") {
      8       @ContentAndMedia {}
      9       @Steps {
     10         @Step {
     11           Starting with a new file, we will create our very first Flatbuffer.
     12           @Code(name: "ViewController.swift", file: "swift_code_1.swift")
     13         }
     14         @Step {
     15           First, we need to import ``FlatBuffers``
     16           @Code(name: "ViewController.swift", file: "swift_code_2.swift")
     17         }
     18         @Step {
     19           We need to create an instance of the `FlatBufferBuilder`, which will contain the buffer as it grows. 
     20           You can pass an initial size of the buffer (here 1024 bytes), which will grow automatically if needed.
     21           @Code(name: "ViewController.swift", file: "swift_code_3.swift")
     22         }
     23         @Step {
     24           After creating the builder, we can start serializing our data. Before we make our orc Monster, 
     25           let's create some Weapons: a Sword and an Axe. However we will start by naming our weapons as `Sword` and `Axe`
     26           @Code(name: "ViewController.swift", file: "swift_code_4.swift")
     27         }
     28         @Step {
     29           After naming the weapons, we will create two weapon objects with the damage that the weapon is going to deal. 
     30           That's done by calling the `start` Method on each table you will be creating, in this case its called `startWeapon` 
     31           and finished by calling `end`.
     32           @Code(name: "ViewController.swift", file: "swift_code_5.swift")
     33         }
     34         @Step {
     35           We will take our (Sword and Axe) serialized data and serialize their offsets as a vector of tables into our `ByteBuffer`. 
     36           So we can reference them later on from our Monster Object
     37           @Code(name: "ViewController.swift", file: "swift_code_6.swift")
     38         }
     39         @Step {
     40           We will add our Monster name as a string value just like we did with the weapons.
     41           @Code(name: "ViewController.swift", file: "swift_code_7.swift")
     42         }
     43         
     44         @Step {
     45           We will create a path that our monster should be using while roaming in its den. To create a vector of paths we would us
     46           `createVector(ofStructs: [])` which will take a Native `Swift` struct that has been padded to fit the `FlatBuffers` standards.
     47           
     48           There are usually two ways of creating vectors in `FlatBuffers` which you can see in commented out code. 
     49           And thus there are multiple convenience methods that will cover all the bases 
     50           when trying to create a vector so that you dont have to create it with `start` and `end`
     51           @Code(name: "ViewController.swift", file: "swift_code_8.swift")
     52         }
     53         
     54         @Step {
     55           Now to serialize our data into our `Monster` object. Which again there are two ways of doing, by calling the `create` method or
     56           by serializing the objects yourself. What we added to our Monster were the `Equipped Type` and the `Equipped` union itself, which
     57           allows the Monster to have the `Axe` as his equipped weapon.
     58           
     59           Important: Unlike structs, you should not nest tables or other objects, 
     60           which is why we created all the `strings/vectors/tables` that this monster refers to before start.
     61           If you try to create any of them between start and end, you will get an `assert`.
     62           @Code(name: "ViewController.swift", file: "swift_code_9.swift")
     63         }
     64         
     65         @Step {
     66           Finally you can just finalize the buffer by calling `builder.finish` and get the Byte array from the buffer.
     67           @Code(name: "ViewController.swift", file: "swift_code_10.swift")
     68         }
     69 
     70       }
     71     }
     72   }