1、官方項(xiàng)目地址:https://github.com/apple/swift-protobuf
2、拉取項(xiàng)目到本地
$ git clone https://github.com/apple/swift-protobuf.git
3、進(jìn)入項(xiàng)目本地路徑,查看版本
$ cd swift-protobuf

image.png
4、根據(jù)項(xiàng)目需要,選擇版本編譯 如:1.5.0
$git checkout tags/1.5.0
5、構(gòu)建項(xiàng)目
$swift build -c release -Xswiftc -static-stdlib
6、構(gòu)建完成之后會(huì)生成一個(gè)protoc-gen-swift執(zhí)行路徑:swift-protobuf/.build/release/protoc-gen-swift

image.png
7、把protoc-gen-swift文件直接拷貝到/usr/local/bin目錄下
cd /usr/local/bin/

image.png
8、swift版本的protobuf已安裝完成,現(xiàn)在可以來測試使用
準(zhǔn)備一個(gè)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文件拖入到項(xiàng)目中就可以使用了

image.png