Environment Installation: PB compiler Installation
1, download Protocolbuffer corresponding version of the compiler package from https://github.com/google/protobuf/releases, such as the current corresponding OBJC the latest version Protocol buffers v3.2.0
2, unpack the package, assuming the decompression, open the terminal, go to the folder Protobuf-objectivec, execute the following command in turn:
./Configuremakemake checksudo make install
If the compilation is successful, you can use the PROTOC command, and later you can use this command to convert the. proto file to a different language source code file.
Second, create a new project, import PB Library, write proto file compilation, use process
1, from https://github.com/google/protobuf.git download source code, decompression, there is support for different languages version of the source code,
All we need is the support inside the OBJC folder named: Objectivec,
2, a new project named Tan_protocolbuffer, create a folder, assuming that the name is: Protocolbuf, the first step of the PB source code to copy into this folder:
3. Using Xcode to open the project, click "+" in the lower left corner to refer to the Protocolbuffer source file to the project
(There are two project profiles in the source file: Protocolbuffers_ios.xcodeproj and protocolbuffers_osx.xcodeproj, reference Protocolbuffers_ Ios.xcodeproj is good)
4, selected project: targets-> Build phases-> Link Binary with Libraries, referencing the source static library file: LIBPROTOCOLBUFFERS.A
5, in the project Settings search static library header files (PB files in the Protocolbuf folder):
targets-> Build setting-> Search paths-> Header search Paths, write: $ (PROJECT)/protocolbuf
6, write the test file *.proto
Create a new file Person.proto, write the common code according to the official standard syntax https://developers.google.com/protocol-buffers/ After writing in the terminal into the directory where the Person.proto file is located, use the PROTOC command: Protoc *.proto--objc_out=. /pro_out to compile. The Person.proto files are compiled into person.pbobjc.h and PERSON.PBOBJC.M, and the resulting files are in the Pro_out folder. If there is a syntax problem with the Person.proto source file, the compilation will be error-free, and when there is no hint at compile time, the compilation is successful: the compiled *.pbobjc.h and *.PBOBJC.M are dragged into the project because arc is not supported, so it needs to be in the *. PBOBJC.M set the-fno-objc-arc, then you can use it.
As follows:
Syntax ="Proto3"; message person{stringName =1; Int32 Age=2; enumdevicetype{IOS=0; Android=1; WP=2; } DeviceType DeviceType=3; Message result{stringURL =1; stringtitle =2; } Repeated Result results=4; Repeated Animal animals=5;} Message animal{floatWeight =1; DoublePrice =2; stringName =3;}
7, write the test code, using the person class for data serialization and deserialization:
Example Demo:https://github.com/xiaotanit/tan_protocolbuffer
iOS Protocolbuffer Build