Git Product home page Git Product logo

moo24 / swiftflatbuffers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from clementpadovani/swiftflatbuffers

0.0 2.0 0.0 1.27 MB

SwiftFlatBuffers is a Swift implementation of FlatBuffers.

License: Apache License 2.0

Swift 28.05% Objective-C 0.04% Objective-C++ 0.19% C++ 24.90% Ruby 0.08% Shell 1.18% Python 6.48% CMake 0.94% Makefile 0.61% Batchfile 0.24% HTML 0.12% CSS 0.22% Go 6.36% Java 6.21% C 0.07% JavaScript 5.72% C# 8.27% PHP 6.70% TypeScript 3.59%

swiftflatbuffers's Introduction

Introduction

FlatBuffers is an efficient cross platform serialization library for games and other memory constrained apps. It allows you to directly access serialized data without unpacking/parsing it first, while still having great forwards/backwards compatibility.

SwiftFlatBuffers is a Swift library for FlatBuffers which support most FlatBuffers feature with firendly interface. 1.1.0 version starts to be compatible with Linux, Carthage and Swift Package Manager.

Usage

Generate Source code

Build flatc

Open SwiftFlatBuffers.xcworkspace and set the active schema to flatc then run. Find binary file flatc in the folder which was named flatc.

Write a schema

schema syntax

// example schema
attribute "priority";

enum Color:byte (bit_flags) { Red = 0, Green, Blue = 3 }

union Any { Monster, Stat }

struct Vec3 (force_align: 16) {
  x:float;
  y:float;
  z:float;
  test1:double;
  test2:Color;
}

struct Ability {
  id:uint(key);
  distance:uint;
}

table Stat {
  id:string;
  val:long;
  count:ushort;
}

table Monster {
  pos:Vec3 (id: 0);
  hp:short = 100 (id: 2);
  mana:short = 150 (id: 1);
  name:string (id: 3, required, key);
  color:Color = Blue (id: 6);
  inventory:[ubyte] (id: 5);
  friendly:bool = false (deprecated, priority: 1, id: 4);
  testarrayoftables:[Stat] (id: 7);
  testarrayofstring:[string] (id: 8);
  testarrayofbools:[bool] (id: 9);
  testarrayofsortedstruct:[Ability] (id: 10);
  test:Any (id: 12);
}

root_type Monster;

Generate

include namespace:

./flatc -sw xxx.fbs

with namespace:

./flatc -sw xxx.fbs --sw-namespace

Installation

CocoaPods:

pod "SwiftFlatBuffers"

Carthage:

github "TonyStark106/SwiftFlatBuffers" ~> 1.2.2

Swift Package Manager:

.Package(url: "https://github.com/TonyStark106/SwiftFlatBuffers.git", majorVersion: 1)

Code

import SwiftFlatBuffers

// Serialize
let m = Monster()
m.name = "Tony"
m.hp = 255
m.testarrayofstring = ["Spitfire", "coding"]
let data = m.toFBData()

// Deserialize
if let n = Monster(root: data) {
  print("Monster name:    \(n.name!)")
  print("Monster hp:      \(n.hp)")
  print("Monster skills:  \(n.testarrayofstring!)")
}

Example

  1. Write your schema files and save them to fbs folder.
  2. Run sh build_flatbuffers.sh will auto generate your source code and build in MyFlatBuffers.framework.
  3. add import MyFlatBuffers in Example project and try your generated source code.

Unit Test

Open SwiftFlatBuffers.xcworkspace and set the active schema to Example then hit Command + U.

ExampleTests.swift

Licensing

SwiftFlatBuffers is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

swiftflatbuffers's People

Contributors

clementpadovani avatar tonystark106 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.