Getting started Protocol Buffers in Swift

In this post, trial the Protocol Buffer on Swift.



If you want to use Protocol Buffer in Swift, try the swift-protobuf.

Usage and references are here:

Easy to use

swift-protobuf is a Swift Package, so you can add it with Xcode.

As an example BookInfo.proto, write the data definition in a file.

emacs BookInfo.proto

A sample is listed on the project page, so let’s copy it.

syntax = "proto3";

message BookInfo {
   int64 id = 1;
   string title = 2;
   string author = 3;

Next, let ‘s create a Swift file from BookInfo.proto

A tool to create Swift files can be installed via Homebrew.

brew install swift-protobuf
protoc --swift_out=. BookInfo.proto

To generate a Swift file with the following command.

The generated result is here.

// swift-format-ignore-file
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: BookInfo.proto
// For information on using the generated types, please see the documentation:

import Foundation
import SwiftProtobuf

// If the compiler emits an error on this type, it is because this file
// was generated by a version of the `protoc` Swift plug-in that is
// incompatible with the version of SwiftProtobuf to which you are linking.
// Please ensure that you are building against the same version of the API
// that was used to generate this file.
fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {
  struct _2: SwiftProtobuf.ProtobufAPIVersion_2 {}
  typealias Version = _2

struct BookInfo {
  // SwiftProtobuf.Message conformance is added in an extension below. See the
  // `Message` and `Message+*Additions` files in the SwiftProtobuf library for
  // methods supported on all messages.

  var id: Int64 = 0

  var title: String = String()

  var author: String = String()

  var unknownFields = SwiftProtobuf.UnknownStorage()

  init() {}

// MARK: - Code below here is support for the SwiftProtobuf runtime.

extension BookInfo: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
  static let protoMessageName: String = "BookInfo"
  static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
    1: .same(proto: "id"),
    2: .same(proto: "title"),
    3: .same(proto: "author"),

  mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
    while let fieldNumber = try decoder.nextFieldNumber() {
      // The use of inline closures is to circumvent an issue where the compiler
      // allocates stack space for every case branch when no optimizations are
      // enabled.
      switch fieldNumber {
      case 1: try { try decoder.decodeSingularInt64Field(value: & }()
      case 2: try { try decoder.decodeSingularStringField(value: &self.title) }()
      case 3: try { try decoder.decodeSingularStringField(value: & }()
      default: break

  func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
    if != 0 {
      try visitor.visitSingularInt64Field(value:, fieldNumber: 1)
    if !self.title.isEmpty {
      try visitor.visitSingularStringField(value: self.title, fieldNumber: 2)
    if ! {
      try visitor.visitSingularStringField(value:, fieldNumber: 3)
    try unknownFields.traverse(visitor: &visitor)

  static func ==(lhs: BookInfo, rhs: BookInfo) -> Bool {
    if != {return false}
    if lhs.title != rhs.title {return false}
    if != {return false}
    if lhs.unknownFields != rhs.unknownFields {return false}
    return true

The code in following:

var unknownFields = SwiftProtobuf.UnknownStorage()

More details on this are below:

Now let’s pass this data to C# (such as Azure Functions) and exchange it.


Let's share this post!