iOS environment using the configuration protobuf

Protobuf need HomeBrew configuration tool or is MacPort. If not installed, or the need to configure the tool is HomeBrew MacPort.

Step 1 (preparatory work before the environment configuration):

         1: HomeBrew 

              brew install autoconf

               brew install automake

               brew install libtool

        2: Use MacPort (used HomeBrew, please ignore this step is of course also possible to perform this step.)

               sudo port install autoconf automake libtool

Step 2 (download Google's protobuf library):

           C ++ is based on download protobuffer: protobuffer

Step 3 (.proto documents translated into the generation tools oc class):

         1: cd to the directory where downloaded files (the following steps need to be handled in the case of vpn, if not open, there is a solution online)

          E.g. cd / Users / macxu / Desktop / protobuf-master

          2: Input

           ./autogen.sh

           3: Enter

           ./configure

           4: Enter

          make

          # If you want to install protoc, execute the following command  

           make install

      (Best generation compiler tools)

(Use protobuf project) Step 4:

    1: Create a proto file specified data format, you can choose proto2 and proto3 format, they are some subtle differences, when the generated code will prompt the specific circumstances view the document LanguageGuide proto3. Used below proto3 format and is saved as Person.proto. (Please generated in the windows environment following text, do not seem to be able to generate the next mac platform. Oc when compiled seems to be an error)

syntax = "proto3";

message   Person {

           string name = 1;

          int32 uid = 2;

          string email = 3;

         enum PhoneType {

               MOBILE = 0;

              HOME = 1;

              WORK = 2;}

message PhoneNumber {

           string number = 1;

           PhoneType type = 2;}

repeated PhoneNumber phone = 4;}

2: Use protoc tool to generate Objective-C code. Wherein the document file --proto_path = proto followed by the folder to be processed, - objc_out = specified build is Objective-C code and the target file storage path, the last file to be processed. (Objc_out File on the generation OC)

protoc --proto_path=. --objc_out=. Person.proto

3: After processing, two files are generated, and are Person.pbobjc.h Person.pbobjc.m. These two files are used by the manual reference counters need to set their translation parameters after the addition of the project.

-fno-objc-arc

4: In order to facilitate the management, we will direct Protocol Buffers in the coming introduction of iOS static library project

 

(Ps: there may be a project under the mac, please manually delete) (best to create a new folder in the new project directory, to add the project to be added such as protoTool folder).

 

5: Adding a static library project in the works below,

 

6: Set-dependent and connection libraries.

 
 

7: Remember to set Header Search Paths or User Header Search Paths. Adding to go in the path of the file resides on the local level folder.

 

And now you can use the same general category of oc use Person.pbobjc.h.

8: use of specific codes:

Person *person = [[Person alloc] init];

person.name= @"Zhangsan";

person.email= @"[email protected]";

person.uid=23;

NSData *data = [person data];

NSString*path = @"/Users/apple/Desktop/test.data";

[data writeToFile:path atomically:YES];

NSData *ldata = [NSData dataWithContentsOfFile:path];

Person *p = [Person parseFromData:ldata error:nil];

ps: add native libraries when there are possible solutions to problems encountered:

1: not previously been directly added to the project with a static library. When added directly to the need to add the project dragged into is wrong, wrong, wrong. You should add files to add.

2: When you add a header search, the problem can not find the path appears. We need to add the path to the folder where the project path added a layer file.

 

Transfer: https: //www.jianshu.com/p/e835376b2ba9

Guess you like

Origin www.cnblogs.com/luoluosha/p/11671337.html