|
|
9 년 전 | |
|---|---|---|
| Documentation | 9 년 전 | |
| Performance | 9 년 전 | |
| Protos | 9 년 전 | |
| Reference | 9 년 전 | |
| Sources | 9 년 전 | |
| SwiftProtobuf.xcodeproj | 9 년 전 | |
| Tests | 9 년 전 | |
| .gitignore | 9 년 전 | |
| .jazzy.yaml | 9 년 전 | |
| CollectTests.awk | 9 년 전 | |
| CopyrightFixup.sh | 9 년 전 | |
| LICENSE.txt | 9 년 전 | |
| Makefile | 9 년 전 | |
| Package.swift | 9 년 전 | |
| README.md | 9 년 전 | |
| SwiftProtobuf.podspec | 9 년 전 | |
| failure_list_swift.txt | 9 년 전 |
:warning: WARNING :warning: This project is in a prerelease state. There is active work going on that will result in API changes that can/will break code while things are finished. Use with caution.
Welcome to Swift Protobuf!
Apple's Swift programming language is a perfect complement to Google's Protocol Buffer serialization technology. They both emphasize high performance and programmer safety.
This project provides both the command-line program that adds Swift
code generation to Google's protoc and the runtime library that is
necessary for using the generated code.
After using the protoc plugin to generate Swift code from your .proto
files, you will need to add this library to your project.
More information is available in the associated documentation:
protoc-gen-swift
plugin that adds Swift support to the protoc programIf you've worked with Protocol Buffers before, adding Swift support is very
simple: you just need to build the protoc-gen-swift program and copy it into
your PATH.
The protoc program will find and use it automatically, allowing you
to build Swift sources for your proto files.
You will also, of course, need to add the Swift runtime library to
your project.
To use Swift with Protocol buffers, you'll need:
A recent Swift 3 compiler that includes the Swift Package Manager. The Swift protobuf project is being developed and tested against the release version of Swift 3.0 available from Swift.org
Google's protoc compiler. The Swift protoc plugin is being actively developed
and tested against the latest protobuf 3.x sources; in particular, the tests need a version
of protoc which supports the swift_prefix option. It may work with earlier versions
of protoc. You can get recent versions from
Google's github repository.
Building the plugin should be simple on any supported Swift platform:
$ git clone https://github.com/apple/swift-protobuf.git
$ cd swift-protobuf
Pick what released version of SwiftProtobuf you are going to use. You can get a list of tags with:
$ git tag -l
Once you pick the version you will use, set your local state to match, and build the protoc plugin:
$ git checkout tags/[tag_name]
$ swift build
This will create a binary called protoc-gen-swift in the .build/debug
directory. To install, just copy this one executable anywhere in your PATH.
To generate Swift output for your .proto files, you run the protoc command as
usual, using the --swift_out=<directory> option:
$ protoc --swift_out=. my.proto
The protoc program will automatically look for protoc-gen-swift in your
PATH and use it.
Each .proto input file will get translated to a corresponding .pb.swift
file in the output directory.
swift buildAfter copying the .pb.swift files into your project, you will need to add the
SwiftProtobuf library to your
project to support the generated code.
If you are using the Swift Package Manager, add a dependency to your
Package.swift file. Adjust the Version() here to match the [tag_name]
you used to build the plugin above:
dependencies: [
.Package(url: "https://github.com/apple/swift-protobuf.git", Version(0,9,24))
]
If you are using Xcode, then you should:
.pb.swift source files generated from your protos directly to your
projectIf you're using CocoaPods, add this to your Podfile but adjust the :tag to
match the [tag_name] you used to build the plugin above:
pod 'SwiftProtobuf', git: 'https://github.com/apple/swift-protobuf.git', :tag => '0.9.24'
And run pod install.
(Swift 3 frameworks require CocoaPods 1.1 or newer)
If you're using Carthage, add this to your Cartfile but adjust the tag to match the [tag_name] you used to build the plugin above:
github "apple/swift-protobuf" "0.9.24"
Run carthage update and drag SwiftProtobuf.framework into your Xcode.project.
Here is a quick example to illustrate how you can use Swift Protocol Buffers in
your program, and why you might want to. Create a file DataModel.proto with
the following contents:
syntax = "proto3";
message BookInfo {
int64 id = 1;
string title = 2;
string author = 3;
}
message MyLibrary {
int64 id = 1;
string name = 2;
repeated BookInfo books = 3;
map<string,string> keys = 4;
}
After saving the above, you can generate Swift code using the following command:
$ protoc --swift_out=. DataModel.proto
This will create a file DataModel.pb.swift with a struct BookInfo and a
struct MyLibrary with corresponding Swift fields for each of the proto fields
and a host of other capabilities:
Set<> or
Dictionary<>.serializeProtobuf() method returns a Data with
a compact binary form of your data. You can deserialize the data using the
init(protobuf:) initializer..serializeJSON() method returns a flexible JSON
representation of your data that can be parsed with the init(json:)
initializer.And of course, you can define your own Swift extensions to the generated
MyLibrary struct to augment it with additional custom capabilities.
Best of all, you can take the same DataModel.proto file and generate Java,
C++, Python, or Objective-C for use on other platforms. Those platforms can all
then exchange serialized data in binary or JSON forms, with no additional
effort on your part.
If you run into problems, please send us a detailed report. At a minimum, please include:
swift --version)protoc --versiongit log -1 to get the
latest commit ID)