Protobuf in Swift

Dzung Nguyen
Sep 9, 2018 · 3 min read

Protobuf, or called by its original name Protocol Buffers, is Google (not newly) invention that is created to resolve the weakness of XML. It is a method for serialization and deserialization structured data. To have a better sense of what Protobuf is, let’s take a look at this example.

You have been assigned a task from your team lead to implement a function to create and retrieve a list of order. This is the steps that often taken to implement:

  1. Cross team discussion about the requirement and dependencies
  2. Create a prototype for data structure to communicate between frontend and backend
  3. After we have agreement on data structure, each team will proceed with their own implementation
  4. Integration

What’s the problem here? It’s step 3, where each team will have different implementation. Because we only have the defined data structure as the common source of trusted, the team will have to create data model by themselves. In my experience, the communication contract is changed quite often during implementation phase, sometimes it’s 100%-off from initial agreement (trust me, it’s the truth). Hence, each time the contract is changed, both team will have to modify their data model. And because it’s human process, we can’t avoid error (I personally don’t trust any human process 100%). We may have error, or the data model is not organized well, or not optimized correctly. That’s why we need something to auto-generate data model when we have the template for communication. Here comes Protobuf as a lifebouy.

Protobuf has an interface description language that we can use to define the template and then we will use the tool to generate source code (data model) for correspond programming languages. At the time of this post, Protobuf supports C++, C#, Java, Python, Objective C and Go. Currently Swift is not supported by Google, but we can still use it by using SwiftProtobuf library created by Apple https://github.com/apple/swift-protobuf

Let’s start by a step to step how to use SwiftProtobuf

Install Protobuf

You can check for latest Protobuf release at this page: https://github.com/protocolbuffers/protobuf/releases. If you are familiar with C++, you can compile manually. But I’m lazy so I will suggest to use the pre-compiled version. For Mac user, we will download protoc-xxx-osx-x86_64.zip.

Unzip the file, you should find a protoc file inside bin folder. Copy this file into /usr/local/bin in your local. Done. You finish installing Protobuf.

Install SwiftProtobuf

We will follow the instructions in https://github.com/apple/swift-protobuf. First, we will clone the repository

$ git clone https://github.com/apple/swift-protobuf.git
$ cd swift-protobuf

Checking version using this command

$ git tag -l

For now, you can check out version 1.1.1, and then build the plugin

$ git checkout tags/1.1.1
$ swift build -c release -Xswiftc -static-stdlib

Above command will create a binary file called protoc-gen-swift in .build/release directory. Copy this file into /usr/local/bin. Done. Now you both installed Protobuf and SwiftProtobuf.

Let’s jump into coding example. Create Test project, then simply use CocoaPods to install SwiftProtobuf into the project

pod 'SwiftProtobuf'

Now we come to the last part, generate code and use it.

We will create a new file, called UserModel.proto. There, we will create the template for our model in Protobuf syntax.

Open Terminal, direct to the path where we store this proto file and type in terminal this command:

$ protoc — swift_out=. UserModel.proto

After entering this command, you will see a Swift file with name UserModel.pb.swift. This is auto-generated file created by Protobuf. We can use our User model now. Let’s create a default user and change its values later

Or we can create an user by passing initial value

We can play some tricks with this model

Done. We complete a simple example of how to use Protobuf in Swift. Hope you guys enjoy!

Reference links:

Dzung Nguyen

Written by

I’m a full-time dreamer, and also a developer. My Git: https://github.com/dzungnguyen1993

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade