Use protobuf from scratch in iOS

Source: Internet
Author: User
Tags install brew

Use protobuf from scratch in iOS
 
There is protobuf-objc on github, and readme can teach us to install proto into our computer. Then, the protoc, that is, the protobuf compiler can compile the. proto file to generate some. h and. m files. In mobile apps, protobuffer can be used for storage, network transmission, and data processing. The simplest is to add an APP. You need to record user data, right? What is the user account? What is the password? Gender? Is there a girlfriend? An appropriate data structure is required to record the data to the server. Some people say why protobuf must be used? Why not use json? Such as xml? For this question, please go to stackoverflow, csdn, and other places with high styles to find and discover .. The reason why I want to use it is that I need to use it. If I don't need it, I will lag behind and I will be beaten ..
First, how to install the protobuf project. (Excerpt from https://github.com/alexeyxo/protobuf-objc) How To Install ProtobufBuilding the Objective-C Protobuf compiler

  1. Check if you have Homebrew
    brew -v
  2. If you don't already have Homebrew, then install it
    ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
  3. Install the main Protobuf compiler and required tools
    brew install automake
    brew install libtool
    brew install protobuf
  4. (Optional) Create a symlink to your Protobuf compiler.
    ln -s /usr/local/Cellar/protobuf/2.6.1/bin/protoc /usr/local/bin
  5. Clone this repository.
    git clone https://github.com/alexeyxo/protobuf-objc.git
  6. Build it!
    ./build.sh
    What? Why not? It doesn't matter. This is for translation purposes.
    First, open the terminal!
    brew -v

    : Check whether brew is installed on your mac. Brew is in mac OS. Similar to the apt-get function of ubuntu, you can directly enter commands on the terminal and install the program. -V means version.
    ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)

    This sentence is not understandable. It is probably to use the curl tool to access that url, and then download and install brew in the ruby environment.
    brew install automakebrew install libtoolbrew install protobuf

    It is downloaded and installed using brew. Protobuf is what we want, and the other two are dependent libraries.
    git clone https://github.com/alexeyxo/protobuf-objc.git
    ./build.sh

    Download the protobuf-objc project from github, and compile the project in the build script.
    Note: errors may occur during project compilation. Don't panic! Check the compilation error prompt. The general error is that the environment variables and paths are not properly configured, and some things are missing. For example, if the dependency library of the protobuf project is missing, add the path as prompted.
    With the project, we can start to test how to use protobuf.
    Open Xcode! Create a project! There are two ways to add protobuf to your project. One is to add protobuf directly, and the other is to use cocoapod to strongly recommend the latter, because cocoapods can easily manage third-party class libraries, after the project is upgraded, you only need to update the pod in a row ~ Duang ~
    The installation and use of cocoapod is another topic. Read another blog post and add the following sentence in Podfile.
    platform :ios , 6.0pod ProtocolBuffers, ~> 1.9.7

    After saving, go to the terminal and cd to the project,
    pod install

    After a while, cocoapod will help you add the project. In the future, we should open the workspace of the project, because the pod is added as a sub-project.
    You have not finished creating a new folder in your project. Name it as Protobuf and create a new proto file in this folder. For example, to store user information locally, you can create a user. proto file as follows:
    Package csdnblog; message PBUser {required string userId = 1; // user IDoptional string nick = 2; // user nickname optional string avatar = 3; // user profile picture optional string password = 7; optional string email = 8; optional string mobile = 9; // mobile phone number optional string qqOpenId = 10; // QQ IDoptional string sinaId = 11; // SINA UserIDoptional string weixinId = 12; // WeChat UserID}

    The preceding example includes several elements. One is the package name. The concept of package is not found in object c. java contains namespaces in c ++. By the way, the oc usually adds two letters to the front of the database name, which serves almost as a package and serves as the upper-layer organizational structure of the class. For example, the official NS, such as AFNetworking, a third-party class library AF.
    Return to the topic. Write pb files. The second element is that a message is a whole, which contains necessary content and optional content. The detailed proto syntax will not be too long when you try to find another operator.
    After writing proto, the next step is to compile the proto file. The protobuf-objc class library will compile the claimed source code, which is an API for reading and writing proto data. The compilation command is as follows: Open the project first. We recommend that you create a folder as the output path. For example, you can create a new folder Gen under the project to put it in generate. pb. h file &&. pb. m file open terminal cd to project path
    protoc --plugin=/usr/local/bin/protoc-gen-objc person.proto --objc_out=./Gen
     
     

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.