Is the metasyntactic static library for iOS . . .
http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers
. . . compatible with regular old C++ compiled protofiles? I do not want to use the bundled compiler that generates Obj-C.
Is there any way to compile the library supplied by Google for iOS?
Ok. It appears that the metasyntactic library (or any other 3rd party library) is unnecessary in this case. You can just add the Google source directly to your project. I found the following answer from Nicola Ferruzzi in a google discussion group . . .
The original answer is here . . .
http://groups.google.com/group/protobuf/browse_thread/thread/ca4218d7db144252
The content of this answer is included below with images to make a permanent record ...
EDIT
Since trying this again tonight for the first time in a while, I needed a couple more steps in addition to those outlined below (this works with protobuf 2.5.0).
find . -name "*unittest*" -exec rm -rf {} \;
testing
#include <google/protobuf/testing/googletest.h>
instringprintf.cc
EDIT : I had answered this earlier but was deleted by moderator. So I have included some code from the tutorial.
A tutorial which is almost same as the answer posted above - Using Google Protocol Buffers in Objective-C on iOS and the Mac
Follow the steps given in learnvst's answer, and refer the comments for pitfalls. I followed the exact same steps except for
Also, when i did
#import xyz.pb.h
the project wasn't building. When I renamed my .m file to .mm i was able to build. This point is mentioned in the tutorial very subtly :P.Here's some content from the tutorial -
PROTO FILE
ZombieSightingMessage.h
ZombieSightingMessage.mm
EDIT : I am using Xcode 4.5. Even after I followed all the steps I was getting a linker error.
Due to this I couldnt run the code on simulator. But it worked on actual device
You can add support for Google Protocol Buffers to an Xcode 5 project using Cocoapods by adding the following line to your Podfile.
This will place the C++ version of the protobuf code into a Pod for your project. It will also add the
protoc
compiler in the folderPods/GoogleProtobuf/bin/protoc
within your project.You can create a custom build rule in your project that automatically converts the
.proto
files into.ph.{h,cc}
files. Here is how I did that:Setup a build rule to "Process Source files with names matching: *.proto Using Custom Script". The script should include the following:
Set the output files to include the following:
Any
.proto
files you include in your project will now automatically be converted to C++ and then compiled as part of your build.I guess based on the actual question my comment is worth posting as an answer:
I'm using a slightly modified version of the native Obj code generation provided by Booyah
It supports repeated fields out of the box but in order to use ObjC fast enumeration you need to convert the PBArray type (basically a typed c buffer) to an array of NSObjects that it represents - either the NSNumber or protobuf message of the objects. You can see an example of the updated fast enumeration code in this change: . You could also add a category for that on PBArray called toObjects.
I just mark the generated code with
-fno-objc-arc
, but you can get arc and 2.5 support from the booyah pull requests.The directions are pretty good for setup, but if people want more explicit instructions on the category I use, how I built the protobuf-objc plugin, how to get support for Class prefixes (e.g. IXMyProtoMessage instead of MyProtoMessage) or how I generate the code let me know and I'll try to set aside time to write up a post. I'm using it with > 50 proto files with a lot of cross project dependencies.
A weakness of the library is that it doesn't include the typical Protobuf reflection api on the generated code so doing something like converting a message to a NSDictionary would have to do some hacky stuff with the objC runtime (the code doesn't follow typical KV compliance) or write a custom code generator from protos that do have the reflection api (I did this with python + jinja2). Or - better yet and of similar difficulty, add reflection apis to the code generator ;).