Google Protobuf iOS development using

Source: Internet
Author: User
Tags format message automake

  

Brief introduction:

Protobuf that Google protocol buffer is a data encapsulation format protocol;

For example, other commonly used Xml,json format, PROTOBUF advantage is high efficiency, the same data use PROTOBUF storage time is smaller, more convenient;

Website:

https://developers.google.com/protocol-buffers/

Https://github.com/google/protobuf

Use on iOS

Currently the latest version requires xcode7.0+, and does not support arc

1. Download all the code from GitHub

2. Compiling protobuf on Mac may also require Autoconf,libtool,automake,

Please install these three dependent libraries on your Mac first, and use brew to install them.

Brew Install autoconf

Brew Install Libtool

Brew Install Automake

Automake installation may be connected to the wall of the site, at this time from the Internet to find a copy of Google host can be;

3. Unzip the downloaded protobuf-master and open the terminal into the /protobuf-master/objectivec/devtools/directory

Execute sudo sh full_mac_build.sh

Wait for the compilation to complete, if you encounter an error may be the above 3 dependencies or other dependencies are not installed, check it.

4. After the installation is complete, the Protoc executable is generated in the protobuf-master/src/directory

In the future we need to use this build to generate the corresponding platform code for the. proto file;

5. Install the PROTOBUF defined PROTO3 syntax below to generate a test data format, Person.proto file

" Proto3 " ; message person {  string1;   2 ;   string 3 ;}

6. Use Protoc to generate the corresponding platform code for the. proto file; The code to generate OC here

Help for the following PROTOC command

Parse proto_files and generate output based on the options given:-ipath,--proto_path=path Specify the directoryinchwhich to search forimports.                              May is specified multiple times; directories 'll be searchedinchorder. If not given, the current working directory isused. --version Show version info and exit. -H,--help Show Thistext and exit. --encode=message_type Read a text-format message of the given type from standard input and write itinchbinary to standard output. The message type must be definedinchProto_files or their imports. --decode=message_type Read A binary MESSAGE of the given TYPE from standard input and write Itinchtext format to standard output. The message type must be definedinchProto_files or their imports. --Decode_raw Read An arbitrary protocol message from standard input and writ E The raw tag/value pairsinchtext format to standard output. No proto_files should be given whenusing  Thisflag. -Ofile, writes a filedescriptorset (a protocol buffer,--descriptor_set_out=file definedinchDescriptor.proto) containing all of the input files to FILE. --include_imports whenusing--Descriptor_set_out, also include all dependencies of the input filesinch theSet, So, theSet  isself-contained. --include_source_info whenusing--descriptor_set_out, DoNot strip Sourcecodeinfo from the Filedescriptorproto. This resultsinchvastly larger descriptors that include information about the original location of each declinchThe source file as Well assurrounding comments. --dependency_out=file Write a dependency output FILEinchThe format expected by make. This writes the transitiveSetof input file paths to file--error_format=format Set The formatinchwhich to print errors. FORMAT May'GCC'(Thedefault) or'MSVs'(Microsoft Visual Studio format). --print_free_field_numbers Print the free field numbers of the messages definedinchThe given proto files. Groups share the same field number space with the parent Messag E. Extension Ranges is counted asoccupied fields numbers. --plugin=executable specifies a plugin executable to use. Normally, Protoc searches the PATH forplugins, but could specify additional executables not inchThe pathusing  Thisflag. Additionally, executable may is of the form NAME=path,inchwhich CaseThe given plugin name isMapped to the given executable evenifThe executable's own name differs.--cpp_out=out_dir Generate C + +header and source. --csharp_out=Out_dir Generate C # source file. --java_out=out_dir Generate Java source file. --javanano_out=out_dir Generate Java Nano source file. --js_out=out_dir Generate JavaScript source. --objc_out=out_dir Generate Objective C header and source. --php_out=out_dir Generate PHP source file. --python_out=out_dir Generate Python source file. --ruby_out=out_dir Generate Ruby source file.
View Code

Generate OC Code Format command

D/protoc--proto_path=a--objc_out=b C/person.proto

D: directory that represents the PROTOC executable program

A: Represents the directory that is required to process the proto file, as is the build directory.

B: The file that represents the generated code, OC will be born. H and. m here is the directory where the file needs to be generated

C: Represents the required. proto file, that is, PROTOC will generate the corresponding platform code file according to the format defined in this file

The oc,.h and. M can then be put into the Xcode project and not supported by the ARC,ARC project. M Add-fno-objc-arc

7. Use xcode7.3 to create a demo project, Gpbdemo, and set up the project for MRC

Import the PERSON.PBOBJC.H,PERSON.PBOBJC.M of the 6th step into the project

manually import all the. h and. m under the protobuf-master/objectivec/directory into the project and import the Google files from that directory into the project.

In the project Setup header search path to add $ (project_dir)/gbpdemo, or import the above Google folder after the compilation will be out of file connection error;

Remove the GPBPROTOCOLBUFFERS.M from the project (this is the official website said Https://github.com/google/protobuf/tree/master/objectivec)

The project structure after import is as follows

Once the above configuration is complete, the compilation will pass normally.

8. Test using PROTOBUF

Import in Engineering VIEWCONTROLLER.M

#import "GPBProtocolBuffers.h"
#import "Person.pbobjc.h"

The following code

Person *pe =[[Person alloc]init]; Pe.name=@"Jobs"; Pe.age= the; Pe.address=@"Beijing"; //Here is the comparison of efficiency//ProtocbuferNSLog (@"Protocbufer:%@", PE); NSLog (@"%lu", [PE data].length); //JSONNsdictionary *PJ = @{@"name":@"Jobs",                           @" Age":@ the,                           @"Address":@"Beijing"}; NSData*JSD = [nsjsonserialization DATAWITHJSONOBJECT:PJ options:0Error:nil]; NSLog (@"JSON:%@", PJ); NSLog (@"%lu", jsd.length); //XMLNSString *xml =@"<name>jobs</name><age>86</age><address>Beijing</address>"; NSData*xmldata =[XML datausingencoding:nsutf8stringencoding]; NSLog (@"XML:%@", XML); NSLog (@"%lu", xmldata.length); [PE release];

Execution Result:

From the above can be seen:

The same data:

Protubuf: 17 bytes

json:44 bytes

xml:56 bytes

The above is the efficiency of the test use

9. Summary

Data in accordance with the PROTOBUF package can be passed through the network to the background, the same use protobuf processing;

will be very convenient and efficient; background define a copy of the proto data format, and then generate code for other platforms, integration calls are very convenient, also biased to maintenance and expansion;

Update the data format for PROTOBUF, and the combined syntax can refer to the Official document description

10: The above IOS sample project

Https://github.com/cocoajin/TDDDemo/tree/master/GBPDemo

Reference:

Https://github.com/google/protobuf

http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/

https://my.oschina.net/kgdugyiy/blog/538333

Google Protobuf iOS development using

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.