Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT</strong> : I had answered this earlier but was deleted by moderator. So I have included some code from the tutorial.</p> <p>A tutorial which is almost same as the answer posted above - <a href="http://www.kotancode.com/2012/10/14/using-google-protocol-buffers-in-objective-c-on-ios-and-the-mac/">Using Google Protocol Buffers in Objective-C on iOS and the Mac</a></p> <p>Follow the steps given in learnvst's answer, and refer the comments for pitfalls. I followed the exact same steps except for </p> <blockquote> <p>add the directory of google headers to your additional include directories I added the <strong><em>src/</em></strong> directory in the header search paths, instead of the google directory.</p> </blockquote> <p>Also, when i did <code>#import xyz.pb.h</code> the project wasn't building. When I renamed my <strong>.m</strong> file to <strong>.mm</strong> i was able to build. This point is mentioned in the tutorial very subtly :P.</p> <blockquote> <p>Basically, any <strong>.m</strong> file which is importing a any <strong>.pb.h</strong> file, should be renamed with extension <strong>.mm</strong></p> </blockquote> <p>Here's some content from the tutorial - </p> <p><strong>PROTO FILE</strong></p> <pre><code>package kotancode; enum ZombieType { SLOW = 0; FAST = 1; } message ZombieSighting { required string name = 1; required double longitude = 2; required double latitude = 3; optional string description = 4; required ZombieType zombieType = 5 [default = SLOW]; } </code></pre> <p><strong>ZombieSightingMessage.h</strong> </p> <pre><code>// -- ZombieSightingMessage.h - note my C++ object is not in the public interface. #import &lt;Foundation/Foundation.h&gt; @interface ZombieSightingMessage : NSObject - (void)doSomething; @end </code></pre> <p><strong>ZombieSightingMessage.mm</strong></p> <pre><code>// -- ZombieSightingMessage.mm #import &lt;UIKit/UIKit.h&gt; #import "ZombieSightingMessage.h" #import "zombie.pb.h" @implementation ZombieSightingMessage - (void)doSomething { // Doing random stuff with a UIView here to show the mixing // of C++ and Objective-C/Cocoa syntax in the same file... UIView *uiView = [[UIView alloc] init]; [uiView setCenter:CGPointMake(20, 10)]; // instantiate my protobuf-generated C++ class. kotancode::ZombieSighting *zombieSighting = new kotancode::ZombieSighting(); zombieSighting-&gt;set_name("Kevin"); zombieSighting-&gt;set_description("This is a zombie"); zombieSighting-&gt;set_latitude(41.007); zombieSighting-&gt;set_longitude(21.007); zombieSighting-&gt;set_zombietype(kotancode::ZombieType::FAST); // Some small tomfoolery required to go from C++ std::string to NSString. std::string x = zombieSighting-&gt;DebugString(); NSString *output = [NSString stringWithCString:x.c_str() encoding:[NSString defaultCStringEncoding]]; NSLog(@"zombie: %@", output); // Instantiate another zombie from the previous zombie's raw bytes. NSData *rawZombie = [self getDataForZombie:zombieSighting]; kotancode::ZombieSighting *otherZombie = [self getZombieFromData:rawZombie]; // Dump the second zombie so we can see they match identically... NSString *newOutput = [NSString stringWithCString:otherZombie-&gt;DebugString().c_str() encoding:[NSString defaultCStringEncoding]]; NSLog(@"other zombie: %@", newOutput); // Grimace all you want, but this is C++ and we need to clean up after ourselves. free(zombieSighting); free(otherZombie); } // Serialize to NSData. Note this is convenient because // we can write NSData to things like sockets... - (NSData *)getDataForZombie:(kotancode::ZombieSighting *)zombie { std::string ps = zombie-&gt;SerializeAsString(); return [NSData dataWithBytes:ps.c_str() length:ps.size()]; } // De-serialize a zombie from an NSData object. - (kotancode::ZombieSighting *)getZombieFromData:(NSData *)data { int len = [data length]; char raw[len]; kotancode::ZombieSighting *zombie = new kotancode::ZombieSighting; [data getBytes:raw length:len]; zombie-&gt;ParseFromArray(raw, len); return zombie; } @end </code></pre> <p><strong>EDIT</strong> : I am using Xcode 4.5. Even after I followed all the steps I was getting a linker error.</p> <blockquote> <p>symbols not found for architecture i386</p> </blockquote> <p>Due to this I couldnt run the code on simulator. But it worked on actual device</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload