IOS IM development preparation (2) install and use protobuf-objc, iosprotobuf-objc

Source: Internet
Author: User

IOS IM development preparation (2) install and use protobuf-objc, iosprotobuf-objc

Google Protocol Buffer (Protobuf) is a hybrid language data standard within Google. You can download Protobuf source code on the http://code.google.com/p/protobuf/downloads/list (however, you need FQ access ). Protocol Buffers is a lightweight and efficient structured data storage format that can be used for serialization or serialization of structured data. It is suitable for data storage or RPC data exchange formats. It can be used for language-independent, platform-independent, and scalable serialized structure data formats in communication protocols, data storage, and other fields. Currently, APIs in C ++, Java, and Python are provided. That is to say, the OC version of Google itself is not provided, some friends said, iOS can directly use the C ++ version, but as a 0 warning chaser, I don't use it.

As for why protobuf is used, it is silly to say that boss is used. High-end, high transmission efficiency and conciseness. In fact, the same traffic can transmit more data. It is said that wechat is also used.

Protobuf-objc has several open-source projects on Github. I use https://github.com/alexeyxo/protobuf-objc. The Readme in it is still hard to understand, at least I am in the trap. Then I found him at http://protobuf.io/?objc, which is simple and comfortable.

The first method of installation and download is to use the command line to download the source code to install the plug-in. I like this.

Installing homebrew In the first line is quite useful. If this line reports an error, check whether the network is good or not.

The second line installs automake. After downloading the source code, you will see makefile in it. If you know it, you will naturally understand it. If you don't know it, you just need Baidu.

The third line is to install libtool, a general-purpose library supporting script, which should be regarded as GCC or make. Ask du Niang if you don't understand

Step 4 install protobuf

You 'd better repeat the command line on the fifth line, or you will regret it and don't enter the command line.

The sixth row is time-consuming.

The seventh line is the compilation command. You need to enter the protobuf-objc folder downloaded in your root directory to execute the command.

Row 8 shows the ProtocolBuffers In the runtime in the src directory in the protobuf-objc folder under the root directory. drag the xcodeproj project to your project (copy items)

 

The second kind of CocoaPods is not suitable for direct people like me + _ +...

From 1 to 7 of the first method, you still need to use the following three rows of pods. If you don't understand them, you may think about it...

The third method is actually a more direct method I have found. Download the top compressed package directly at http://protobuf.io/?objc

I do not recommend this. If you fail to do so, you can still use the first two methods...

 

Compile. proto

After all, the installation is only a tool. I won't talk about the syntax of proto, too much. I suppose someone gave you the. proto file. Now you need to convert it into an objc file.

First, open the terminal cd to the Desktop to facilitate observation of the operation results;

Enter this line of protoc -- plugin =/usr/local/bin/protoc-gen-objc person. proto -- objc_out = "./pbDir"

The red one is your proto file. I put it on the desktop by default; the blue one is the directory where the generated file is to be stored. /The pbDir in the current directory is the folder I created to store the generated objc file.

Each proto file generates two files, one. h and one. m.

Finally, drag your generated file to your project and use it.

 

Brief description of proto Message

Each package in the proto file is a message. Let me explain a complete example.

 1 message MTMsg  2 { 3     enum Type 4     { 5         ax = 0; 6         ay = 1; 7         aa = 2; 8         ab = 3; 9     }10     required Type ntype = 1;11     repeated TKSMsg objs = 2 ;12     optional string name = 3 ;13 }    

It can be seen that there is an enumeration in this message, isn't it amazing. Let's look at the first variable, which is an enumeration field. The preceding required indicates that it is a required field. The second variable is a custom TKSMsg (which is also a message ), the preceding repeated indicates that it is an array type; the last one is a string, and optional indicates that it is an optional field.

After compilation, the message will become very large and I will not post it.

After compilation, you can use XXXBuilder to alloc an instance a and assign values to the attribute of this. Then, use a. build to provide a XXX instance B. This B can be used for transmission.

Related Article

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.