5.35-dev) requires Objective-C 2.0 and Xcode 13.3.1 or later. It targets 32-bit and 64-bit iOS and 64-bit macOS.
Installation
Install protoc
Download the prebuilt
protoc binary for macOS from the releases page and add it to your $PATH, or install via Homebrew:Generate Objective-C code
Run This generates
protoc with --objc_out:Addressbook.pbobjc.h and Addressbook.pbobjc.m.Adding sources manually (without CocoaPods)
If you are not using CocoaPods, add the runtime sources directly to your project. You have two options: Option 1 — Single amalgamation file:- Add
objectivec/*.h - Add
objectivec/google/protobuf/*.pbobjc.h - Add
objectivec/GPBProtocolBuffers.m
- Add
objectivec/*.h - Add
objectivec/google/protobuf/*.pbobjc.h - Add
objectivec/google/protobuf/*.pbobjc.m - Add
objectivec/*.m(exceptGPBProtocolBuffers.m)
Generating code
The--objc_out flag is the primary way to generate Objective-C code:
objc_class_prefix option
Because Objective-C uses a flat global namespace, it is important to prefix all generated class names. Set this in your.proto file:
ABPerson, ABAddressBook, etc.
protoc options
Pass additional options via--objc_opt as comma-delimited key=value pairs:
--objc_opt keys:
| Key | Description |
|---|---|
generate_for_named_framework | Emits #import <FRAMEWORK/file.pbobjc.h> instead of plain #import. |
named_framework_to_proto_path_mappings_path | Path to a file mapping framework names to proto files. |
runtime_import_prefix | Prefix for #imports of runtime headers in generated files. |
use_package_as_prefix | Derive the ObjC class prefix from the proto package (yes/no). |
package_to_prefix_mappings_path | Path to a file mapping proto packages to ObjC class prefixes. |
headers_use_forward_declarations | Use forward declarations in headers (yes/no, default yes). |
Working with messages
Generated message objects are mutable Objective-C objects. They follow standard Cocoa conventions:Serializing and parsing
Autocreators
The Objective-C runtime uses an autocreator pattern for message, array, and dictionary properties. Accessing an unset message property returns a temporary instance rather thannil. The instance is attached to its parent only when you mutate it:
has<FieldName> property:
Swift interoperability
The Objective-C generated classes and enums can be used directly from Swift code. No additional bridging is required beyond including the generated headers in your Swift bridging header or module map.Key API reference
GPBMessage
Base class for all generated messages. Provides
data, parseFromData:error:, isEqual:, copy, and debugDescription.GPBCodedInputStream / GPBCodedOutputStream
Low-level streaming I/O. For most use cases, use
data and parseFromData:error: on the message class instead.Autocreators
NSString and NSData properties never return nil. Message, array, and dictionary properties return autocreated instances rather than nil. Use
has<FieldName> to test explicit presence.GPBArray / GPBDictionary
Typed array and dictionary containers for repeated fields and map fields. Provide standard collection methods and avoid boxing overhead for primitive types.