Auto-Compile .proto Files

Google’s protobuf compiler with Apple’s swift plugin converts .proto definitions to .swift implementations.

Instead of converting from the console and then including the generated file in your project, you can include the .proto file in your Xcode project, and let Xcode automatically convert it to swift and use the generated file:

  1. Go to your target Build Rules.
  2. Click the + above to add a custom rule: Sources with names matching: *.proto.
  3. Use the following script:

    protoc --proto_path="$INPUT_FILE_DIR" --swift_out="$DERIVED_FILE_DIR" "$INPUT_FILE_PATH"

  4. Add to Output Files using the + below:

    $(DERIVED_FILE_DIR)/$(INPUT_FILE_BASE).pb.swift

  5. Go to Build Phases, to the Compile Sources section, and add your .proto files to the list.

xcode-proto

Leave a Reply