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
- 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
- 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;}
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
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
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
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);}
IOS integrated protobuf, convert proto files