IOS integrated protobuf, convert proto files

Source: Internet
Author: User
Tags install brew automake

Original address: http://blog.csdn.net/hyq4412/article/details/54891038

Additional Homebrew installation Address: https://brew.sh/index_zh-cn.html

PROTOBUF Introduction

Protocol buffer is a data interchange format for Google, already open source on GitHub, and the latest version is now 3.1.0. It is independent of the language, independent of the platform. Google offers implementations in multiple languages: Java, C #, C + +, Go, and Python,objective-c, each with a compiler and library file for that language. Because it is a binary format, it is much faster than using XML for data exchange. It can be used in data communication between distributed applications or in heterogeneous environments. As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas.

Description
    • protobuf3.0.0 above official support objective-c, less than 3.0.0 please ignore or use a third-party conversion tool
    • Development environment: 32bit & 64bit IOS, 64bit OS x,xcode7.0+
    • ARC is not used for performance reasons, but can be called by the ARC code
Steps
    1. Conversion: Convert our well-written Xxx.proto file to objective C file, That is, the XXX.h and xxx.m files, the conversion tool is generated using the Protoc binary file, which needs to be generated by itself, and later describes how to use it to convert objective-c files
    2. Integration: If you include the Protobuf library in your iOS project and the OC file that you generated in step 1
Convert Build Protoc

If you do not have autoconf automake Libtool need to install these several first, here use brew for installation, in the shell to execute the brew install autoconf Automake Libtool, If you do not have brew, please install brew yourself first.
Download the Protobuf library for Objective-c, with the address (https://github.com/google/protobuf/releases), to download the corresponding OBJECTIVE-C version, for example Protobuf-objectivec-3.1.0.zip, unzip.

CD to the downloaded directory, executed in turn:

    • $./autogen.sh
    • $./configure
    • $ make
    • $ make Check
    • $ sudo make install

Re-execute
-objectivec/devtools/full_mac_build.sh

After execution, you will see that the PROTOC binaries are generated in the SRC directory

Using the PROTOC conversion

Create a proto file, such as Person.proto

"proto3";message Person{     int32 age = 1;    string username = 2; string phone = 3;}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

It is important to note whether the syntax rules for Proto are Proto2 or Proto3.
Executed in the SRC directory (the directory where Protoc is located)

protoc --proto_path=... --objc_out=... XXX.proto
    • 1
    • 1

Where Proto_path is the directory where we created the proto file, Objc_out is the objective-c file output path, Xxx.proto is the proto file we created, you can convert multiple proto files at once, and add them to the Xxx.proto.

Example: We create a new two folder in the SRC directory, gen and Protocols folders, Gen for the output directory, protocols for storing proto files, the created Person.proto in the Protocols folder, execute the command

protoc --proto_path=protocols --objc_out=gen protocols/Person.proto
    • 1
    • 1

The person.pbobjc.h and person.pbobjc.m files are then generated under the Gen folder.

Integration

Put the generated ojective-c file (person.pbobjc.h and PERSON.PBOBJC.M of the example above) into the project, if the project uses arc, the complier of the. m (example PERSON.PBOBJC.M) Flags are set to-FNO-OBJC-ARC. (Protobuf is not using arc based on performance reasons)

There are two ways to join the PROTOBUF library

  • The first is the use of Cocoapods integration

    • With Cocoapods integration, there is an off-the-shelf pod that can be used –protobuf, pod search protobuf to see the details, pod content
      :ios, ‘7.1‘pod ‘Protobuf‘, ‘~> 3.1.0‘
      • 1
      • 2
      • 1
      • 2

    It is important to note that Platform:ios, ' 7.1 '
    7.1 And above to import this library, this way advantage is simple operation, the disadvantage is Platform:ios, ' 7.1 ' to 7.1 or more

  • The second is to drag the relevant files into the project.

    • Drag in the relevant file into the project, add all the. h files and. m files (except GPBPROTOCOLBUFFERS.M) (those that begin with GPB) to the project in the Objectivec folder, and the entire Google folder. If arc is used in the project, the Complier flags for all the. m files above will be set to-fno-objc-arc. The advantage of this approach is that it is flexible and does not have a 7.1 constraint. The disadvantage is the operation of the trouble point, if the use of arc, but also to manually add-fno-objc-arc (using Cocoapods integration will be added automatically), remember to add the user Header Search paths to $ (project_dir)/project name/followed by the file address Or the header file will get an error.
Simple to use

Directly on the code

-(void) Viewdidload {[Super Viewdidload]; person *person = [[person alloc] init]; Person.age = 100; person.username = @ "Huang";  Person.phone = @ "10086";  NSData *data = [person data];< Span class= "Hljs-type" >person *p = [person parsefromdata:data Error:nil]; nslog (@ "person:%@", p);}       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

IOS integrated protobuf, convert proto files

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.