#Protocol Buffers for Swift
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
1.ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2.brew install automake
3.brew install libtool
4.brew install protobuf
5.git clone [email protected]:alexeyxo/protobuf-swift.git
6../scripts/build.sh
7.Add ./src/ProtocolBuffers/ProtocolBuffers.xcodeproj
in your project.
protoc person.proto --swift_out="./"
Installation via Carthage
Cartfile:
github "alexeyxo/protobuf-swift"
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 = "[email protected]"
let person = personBuilder.build()
println("\(person)")
person.data() //return NSData
message Perfomance
{
required int32 ints = 1;
required int64 ints64 = 2;
required double doubles = 3;
required float floats = 4;
optional string str = 5;
optional bytes bytes = 6;
optional string description = 7;
}
var originalBuilder = ProtoPerfomance.builder()
originalBuilder.setInts(Int32(32))
.setInts64(Int64(64))
.setDoubles(Double(12.12))
.setFloats(Float(123.123))
.setStr("string")
let original = originalBuilder.build()
var person = Person.parseFromData(bytes) // from NSData
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
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())
#Custom Options
enum AccessControl {
InternalEntities = 0;
PublicEntities = 1;
}
message SwiftFileOptions {
optional string class_prefix = 1;
optional AccessControl entities_access_control = 2 [default = InternalEntities];
optional bool compile_for_framework = 3 [default = true];
}
At now protobuf-swift's compiler is supporting three custom options(file options).
- Class Prefix
- Access Control
- Compile for framework
If you have use custom options, you need to add:
import 'google/protobuf/swift-descriptor.proto';
in your .proto
files.
###Class prefix
This option needs to generate class names with prefix.
Example:
import 'google/protobuf/swift-descriptor.proto';
option (.google.protobuf.swift_file_options).class_prefix = "Proto";
message NameWithPrefix
{
optional string str = 1;
}
Generated class has a name:
final internal class ProtoNameWithPrefix : GeneratedMessage
###Access control
option (.google.protobuf.swift_file_options).entities_access_control = PublicEntities;
All generated classes marks as internal
by default. If you want mark as public
, you can use entities_access_control
option.
option (.google.protobuf.swift_file_options).entities_access_control = PublicEntities;
message MessageWithCustomOption
{
optional string str = 1;
}
Generated class and all fields are marked a public
:
final public class MessageWithCustomOption : GeneratedMessage
###Compile for framework
option (.google.protobuf.swift_file_options).compile_for_framework = false;
This option deletes the string import ProtocolBuffers
of the generated files.
####If you will need some other options, write me. I will add them.
Developer - Alexey Khokhlov
Google Protocol Buffers - Cyrus Najmabadi, Sergey Martynov, Kenton Varda, Sanjay Ghemawat, Jeff Dean, and others