Generating Objective-C Classes from Protocol Buffer

Once you have the Protocol Buffer Compiler (protoc) built, you can use it to compile proto files into headers and implementations in the language of your choice.

For example, I have this pair of proto files defined. This is a StoryRequest:

message StoryRequest {
required string character1 = 1;
required string character2 = 2;
optional string secret_weapon = 3;
}

And this is StoryResponse:

message StoryResponse {
enum ResponseType {
SUCCESS = 0;
FAILED = 1;
}
required ResponseType status = 1;
optional string story = 2;
}

The client will send a StoryRequest to a server with names of two characters and an object, and the server will send back a StoryResponse with a status code and the story that has those two characters using that object, Mad Libs style.

To compile the proto files using protoc, you want to indicate the path of the source files, the directory to output the files to, and indicate the language.

For example, for C++, you will do something like this:
protoc --proto_path=src --cpp_out=build/cpp src/StoryRequest.proto
And for Objective-C, you will do something like this:
protoc --proto_path=src --objc_out=build/objc src/StoryRequest.proto

Note that you want to create build/cpp and build/obj in advance because protoc will not create them. If you are generating only the Objective-C files, fine, keep them in one place. But in this case, we are doing both for demonstration. Since C++ and Objective-C use the same naming conventions for header files, they need to be in separate folders. You will now see StoryRequest.pb.h and StoryRequest.pb.cc in build/cpp and StoryRequest.pb.h and StoryRequest.pb.m in build/objc.

Let’s ignore the implementation files (.cc and .m) since they are just code for what’s declared in the headers. We will now inspect the C++ header. You will see a lot of functions generated for you. They are mostly getters, setters and parsing functions like this:

inline bool has_character1() const;
inline void clear_character1();
static const int kCharacter1FieldNumber = 1;
inline const ::std::string& character1() const;
inline void set_character1(const ::std::string& value);
inline void set_character1(const char* value);
inline void set_character1(const char* value, size_t size);
inline ::std::string* mutable_character1();

And if you open up the Objective-C version of the header, you will see something like this:

- (BOOL) hasCharacter1;
- (NSString*) character1;
- (StoryRequest_Builder*) setCharacter1:(NSString*) value;
- (StoryRequest_Builder*) clearCharacter1;

They are just Objective-C methods equivalent to the C++ ones. As you can see, the proto files are defined regardless of which language you are going to use, and you will compile it into source files in the languages of your choosing.

In the next post, we’ll talk about how to get these files to compile in an Xcode project.

This entry was posted in Protobuf. Bookmark the permalink.

4 Responses to Generating Objective-C Classes from Protocol Buffer

  1. Pingback: Compiling the Protocol Buffer Compiler for Objective-C | Kelvin Kao Dev

  2. Pingback: Linking the Protocol Buffer Static Library into the Xcode Project | Kelvin Kao Dev

  3. Pingback: How to Send a Protobuf from iPhone Client to Server | Kelvin Kao Dev

  4. Pingback: Generating PHP Classes from Protocol Buffer | Kelvin Kao Dev

Leave a Reply

Your email address will not be published. Required fields are marked *