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:
- Go to your target Build Rules.
- Click the + above to add a custom rule: Sources with names matching:
*.proto
. - Use the following script:
protoc --proto_path="$INPUT_FILE_DIR" --swift_out="$DERIVED_FILE_DIR" "$INPUT_FILE_PATH"
- Add to Output Files using the + below:
$(DERIVED_FILE_DIR)/$(INPUT_FILE_BASE).pb.swift
- Go to Build Phases, to the Compile Sources section, and add your
.proto
files to the list.