Swift 安装protobuf
2019-05-22 本文已影响0人
青年fw
1、官方项目地址:https://github.com/apple/swift-protobuf
2、拉取项目到本地
$ git clone https://github.com/apple/swift-protobuf.git
3、进入项目本地路径,查看版本
$ cd swift-protobuf

4、根据项目需要,选择版本编译 如:1.5.0
$git checkout tags/1.5.0
5、构建项目
$swift build -c release -Xswiftc -static-stdlib
6、构建完成之后会生成一个protoc-gen-swift
执行路径:swift-protobuf/.build/release/protoc-gen-swift

7、把protoc-gen-swift文件直接拷贝到/usr/local/bin目录下
cd /usr/local/bin/

8、swift版本的protobuf已安装完成,现在可以来测试使用
准备一个proto文件(Book.proto)
syntax = "proto2";
message BookInfo {
optional int64 id = 1;
optional string title = 2;
optional string author = 3;
}
9、生成swift文件
$ protoc --swift_out=. Book.proto
10、把生成的Book.swift文件拖入到项目中就可以使用了
