This is a sample project used during the "Beyond Xcode: Exploring Apple’s Open-Source Offerings" presentation.
Slides are available at Slides / information from my talks.
This sample project shows how to use the swift-protobuf package to use Protocol Buffer serialization in Swift.
It defines a data structure reprensenting the information on a conference badge and uses that both in Swift and Python code.
It shows the interoperability aspect, the Xcode tool writing data to disk and the Python tool reading it back.
It also shows the backward and forward compatibility features provided by Protobuf.
On macOS, the easiest method is using Homebrew.
First install the Google Protobuf compiler using
brew install protobufthen the Swift extension (coming from swift-protobuf) by running
brew install swift-protobufThe sample contains 2 folders, v1 and v2, showing the evolution of the protobuf specification for the badge data structure.
Each folder contains:
- badge.proto: the protobuf specification of the badge data structure
- badge_pb2.py: the Python code generated from the compilation of the protobuf specification
- read-badge.py: a command line tool that reads a file containing a protobuf serialization of badge data and displays the information read
- WriteBadgeData: an Xcode project for a command-line tool that writes a file with protobuf serialized badge data.
It includes thebadge.pb.swiftfile, generated from the compilation of the protobuf specification
Generate a v1 or v2 file by running the corresponding tool from the Xcode project.
You can then read it back using either versions of the read-badge.py and observe that there is no issue reading an older or a newer version of the file.
python3 read-badge.py /tmp/badge.bin
python3 read-badge.py /tmp/badge-v2.binIf you want to (re-)generate the Python or Swift code from the protobuf spec, use
protoc --python_out=. badge.proto
protoc --swift_out=. badge.proto