IOS ProtocolBuffer setup and demo, iosprotocolbuffer

Source: Internet
Author: User

IOS ProtocolBuffer setup and demo, iosprotocolbuffer

This time I set up the ProtocolBuffer compiler for iOS and encountered many problems when compiling *. proto Source files into *. pbobjc. h and *. pbobjc. m files!

There was no problem when building the pb compiler. After compiling the *. proto file, I used cocoaPods to integrate ProtocolBuffers into my project,

During cocoaPods integration, I tried two libraries:

Https://github.com/google/protobuf.git,

The other one is:

Https://github.com/alexeyxo/protobuf-objc.git

However, an error is always reported during running:

It will be reported that the ProtocolBuffer library file cannot be found;

Report version mismatch later: This file was generated by a different version of protoc which is incompatible with your Protocol Buffer library sources;

The attribute data format does not exist and does not match.

 

Finally, I had to give up CocoaPods and drag the library downloaded from https://github.com/google/protobufinto the project. This is a good deal!

Briefly describe the installation process of the pb compiler and the demo after compilation:

 

I. Environment installation: Install the pb Compiler

1. Slave

For: https://github.com/google/protobuf/releases/download/v3.1.0/protobuf-objectivec-3.1.0.zip


2, extract the package, assuming that the decompressed folder name: protobuf-objectivec-3.1.0,
Open the terminal, enter the folder protobuf-objectivec-3.1.0, and execute the following commands in sequence:

./configuremakemake checksudo make install

If the compilation succeeds, you can use the protoc command to convert the. proto file to source code files in different languages.

 

2. Create a new project, import the pb library, compile the proto file, and use the process

1. Download the source code from https://github.com/google/protobuf.gitand decompress it. The source code that supports different languages appears,

We only need the folder name that supports objc: objectivec,

  

 

2. Create a project named Tan_ProtocolBuffer and create a folder named protocolbuf. Copy the pb source code in step 1 to this folder:

  

 

3. Use Xcode to open the project, click "+" in the lower left corner, and reference the protocolBuffer source file to the project.

4. Select the project: targets-> Build Phases-> Link Binary With Libraries, reference the source code static library file: libProtocolBuffers.

  

 

5. Search for the static library header file in the Project Settings (the pb file is in the protocolbuf folder ):

Targets-> Build Setting-> Search Paths-> Header Search Paths, write: $ (PROJECT)/protocolbuf

  

 

  

6, write the test file *. proto, such as creating a new file Person. proto, according to the official standard syntax https://developers.google.com/protocol-buffers/ to write general code,
Enter the directory of the Person. proto file on the terminal, and use the protoc command: protoc *. proto -- objc_out = ../Pro_out to compile the file.
Compile the Person. proto file into Person. pbobjc. h and Person. pbobjc. m. The generated file is in the Pro_out folder.
If there is a syntax problem with the Person. proto Source file, an error will be reported during compilation. If no prompt is displayed during compilation, the compilation is successful:
Compile *. pbobjc. h and *. pbobjc. m is dragged into the project. Because arc is not supported, you need *. pbobjc. m sets-fno-objc-arc, and then you can use it.

As follows:

    

 

 

 

 

 

 

7. Compile the test code and use the Person class for data serialization and deserialization:

  

Example demo: https://github.com/xiaotanit/Tan_ProtocolBuffer

 

Link: http://www.cnblogs.com/tandaxia/p/6181534.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.