Skip to content

alecthomas/protobuf-swift

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Protocol Buffers for Swift Build Status Platform Release

An implementation of Protocol Buffers in Swift.

Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. This project is based on an implementation of Protocol Buffers from Google. See the Google protobuf project for more information.

####Required Protocol Buffers 2.6

How To Install Protobuf

1.ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.brew install automake

3.brew install libtool

4.brew instal protobuf

5.git clone git@github.com:alexeyxo/protobuf-swift.git

6../scripts/build.sh

7.Add ./src/ProtocolBuffers/ProtocolBuffers.xcodeproj in your project.

Compile ".proto" files.

protoc person.proto --swift_out="./"

Example

message Person {
    required int32 id = 1;
    required string name = 2;
    optional string email = 3;
}
    let personBuilder = Person.builder()
    personBuilder.id = 123
    personBuilder.name = "Bob"
    personBuilder.email = "bob@example.com"
    let person = personBuilder.build()
    println("\(person)")

    person.data() //return [Byte]
    person.getNSData() //Return NSData

Using Oneof

message SubMessage {
    optional string str = 1;
}

message SampleMessage {
  oneof test_oneof {
     string name = 4;
     int32 id = 5;
     SubMessage mes = 6;
  }
}
    var sm = SampleMessage.builder()
    sm.name = "Alex"
    sm.id = 123
    println(ss.build()) //->  id: 123

Nested Types

message SearchResponse {
    message Result {
        required string url = 1;
        optional string title = 2;
        repeated string snippets = 3;
    }
    repeated Result result = 1;
}
    var builderResult = SearchResponse.Result.builder()
    builderResult.url = "http://protobuf.axo.io"
    builderResult.title = "Protocol Bufers Apple Swift"
    var searchRespons = SearchResponse.builder()
    searchRespons.result += [builderResult.build()]
    println(searchRespons.build())

Credits

Developer

  • Alexey Khokhlov

Google Protocol Buffers

  • Cyrus Najmabadi, Sergey Martynov, Kenton Varda, Sanjay Ghemawat, Jeff Dean, and others

About

ProtocolBuffers for Apple Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.1%
  • C++ 1.6%
  • Other 0.3%