Xcode use Protocol buffer

http://stackoverflow.com/questions/10277576/google-protocol-buffers-on-ios

http://stackoverflow.com/questions/8759202/google-protocol-buffers-on-ios-objective-c

 

#### build 2.4.1 failed on mac os x 10.9

I think the right way to fix it is to modify the message.h like below
#ifdef __DECCXX
// HP C++'s iosfwd doesn't work.
#include <iostream>
#else
#include <sstream>
//#include <iosfwd>
#endif

 

Adding #include <istream> to src/google/protobuf/message.cc fixes this problem, as per the latest version of that file in svn.

 

加入 proto文件自动编译

  1. In Xcode 4.2, open the target properties, go to the "Build Rules" tab and create a new build rule for "*.proto" files
  2. In "Using:", choose "custom script" and use a script like this:

    protoc --plugin=/usr/local/bin/protoc-gen-objc $INPUT_FILE_PATH --objc_out=$DERIVED_FILES_DIR

  3. In Output files, add the generated files (there should be two files, $(INPUT_FILE_BASE).pb.mand $(INPUT_FILE_BASE).pb.h.

  4. Add the .proto files to your project.

 

 

To compile and test the easiest way Ive found is to just import the whole google directory in my own project :) (the second time you can make your own framework but for testing this procedure just works)

  • download latest version
  • autogen configure and make like you were just building for macosx (you need command line tools) . This way you end up with protoc
    binary and the library for macosx (which you don't need)
  • open your Xcode iOS project
  • add "new file" to your project and select google directory
  • add the directory of google headers to your additional include directories
  • add config.h from the protobuffer src directory to your app
  • from the google group remove everything that contains unitest :)
  • from the google group remove compiler and java stuff;

You should be able to compile without any linking error. To give you an idea this is what I directly compile

enter image description here

Then you can use protoc to generate c++ source files for your protocol. To use them with objc you have to rename your source to file "mm" then you can do something like

TO SERIALIZE TO NSDATA

let's say your message is called Packet

-(NSData*)getDataForPacket:(Packet*)packet { 
    std::string ps = packet->SerializeAsString();return[NSData dataWithBytes:ps.c_str() length:ps.size()];

TO READ FROM NSDATA

-(Packet*)getPacketFromNSData:(NSData*)data {char raw[[data length]];Packet*p =newPacket;[data getBytes:raw length:[data length]]; 
  p->ParseFromArray(raw,[data length]);return p;}
posted @ 2013-11-07 17:42  翛尧  阅读(2847)  评论(0编辑  收藏  举报