Google protocol buffers on iOS

后端 未结 4 1924
情书的邮戳
情书的邮戳 2020-12-07 17:23

Is the metasyntactic static library for iOS . . .

http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers

. . . compatible with regular old C++ compiled

4条回答
  •  攒了一身酷
    2020-12-07 17:45

    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

    add the directory of google headers to your additional include directories I added the src/ directory in the header search paths, instead of the google directory.

    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.

    Basically, any .m file which is importing a any .pb.h file, should be renamed with extension .mm

    Here's some content from the tutorial -

    PROTO FILE

    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];
    }
    

    ZombieSightingMessage.h

    // -- ZombieSightingMessage.h - note my C++ object is not in the public interface.
    #import 
    
    @interface ZombieSightingMessage : NSObject
    - (void)doSomething;
    @end
    

    ZombieSightingMessage.mm

    // -- ZombieSightingMessage.mm
    #import 
    #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->set_name("Kevin");
        zombieSighting->set_description("This is a zombie");
        zombieSighting->set_latitude(41.007);
        zombieSighting->set_longitude(21.007);
        zombieSighting->set_zombietype(kotancode::ZombieType::FAST);
    
        // Some small tomfoolery required to go from C++ std::string to NSString.
        std::string x = zombieSighting->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->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->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->ParseFromArray(raw, len);
        return zombie;
    }
    
    @end
    

    EDIT : I am using Xcode 4.5. Even after I followed all the steps I was getting a linker error.

    symbols not found for architecture i386

    Due to this I couldnt run the code on simulator. But it worked on actual device

提交回复
热议问题