Protocol Buffer is a lightweight and efficient structured data storage format developed by Google


Swift

I. Environment configuration

Can be installed directly using Brew install swift-protobuf

You can also perform the following steps:

1.Clone Protobuf

Github.com/apple/swift…

$ git clone https://github.com/apple/swift-protobuf.git
$ cd swift-protobuf
Copy the code

2. Select tag and build protoc plug-in

$git tag -l $git tag -l $git tag -l $git tag -lCopy the code

Protoc-gen-swift will be generated in swift-protobuf/.build/release after the build is complete

Then copy protoc-gen-swift to the environment variable path

Second, create.protoFile, define structure

// use PB3 syntax = "proto3"; Message Test {string title = 1; int32 tag = 2; Request request = 3; repeated string values = 4; Message Request {string url = 1; }Copy the code

Three,.protoFiles are converted to the Swift model

  1. Create two foldersorigin_pathandtarget_pathAnd placed in the same pathTestUnder the

(Note: Folder name is customized)

  1. Create the previous step.protoIn theorigin_pathUnder the path

CD to Test, terminal execution :(note the path and file name)

protoc --proto_path=origin_path --swift_out=target_path origin_path/Test.proto
Copy the code
  1. intarget_pathTo view the converted file

4. Use it in iOS projects

  1. Pod add
pod 'SwiftProtobuf'
Copy the code
  1. Add the model file to the project

  2. Code

        /* { "title": "test", "tag": 1, "request": { "url": "www.fivehow.com" }, "values": ["value1", "value2"] } */
        
        let request = Request.with { $0.url = "www.fivehow.com" }
        
        let test = Test.with {
            $0.title = "test"
            $0.tag = 1
            $0.request = request
            $0.values = ["value1"."value2"]}let binaryData = try? test.serializedData()
        guard let binaryData = binaryData else { return }
        _ = try? Test(serializedData: binaryData)
        
        let jsonStr = "{\"title\":\"test\".\"tag\": 1,\"request\": {\"url\":\"www.fivehow.com\"},\"values\": [\"value1\".\"value2\"]}"
        let jsonStrData = jsonStr.data(using: .utf8)
        
        print("binaryData: \(binaryData.count)")            / / 43
        guard let jsonStrData = jsonStrData else { return }
        print("jsonStrData: \(jsonStrData.count)")          / / 92
Copy the code

Objective-C

I. Environment configuration

1. Download Protobuf and decompress it

Github.com/protocolbuf…

CD Go to the decompressed path

2. Terminals perform operations in sequence

./autogen.sh

./configure

make

make check

make install
Copy the code

3. Check whether the installation is successful

protoc --version
Copy the code

Second, create.protoFile, define structure

// use PB3 syntax = "proto3"; // model file prefix option objc_class_prefix = "WH"; Message Test {string title = 1; int32 tag = 2; Request request = 3; repeated string values = 4; Message Request {string url = 1; }Copy the code

Three,.protoFiles are converted into OC models

  1. Create two foldersorigin_pathandtarget_pathAnd placed in the same pathTestUnder the

(Note: Folder name is customized)

  1. Create the previous step.protoIn theorigin_pathUnder the path

CD to Test, terminal execution :(note the path and file name)

protoc --proto_path=origin_path --objc_out=target_path origin_path/Test.proto
Copy the code
  1. intarget_pathTo view the converted file

4. Use it in iOS projects

  1. Pod add
pod 'Protobuf'
Copy the code
  1. Add the model file to the project

  2. Import the model header file

#import "Test.pbobjc.h"
Copy the code
  1. Code
/* { "title": "test", "tag": 1, "request": { "url": "www.fivehow.com" }, "values": ["value1", "value2"] } */ // ProtoBuf data WHTest *test = [[WHTest alloc] init]; test.title = @"test"; test.tag = 1; test.valuesArray = [NSMutableArray arrayWithObjects:@"value1", @"value2", nil]; WHRequest *request = [[WHRequest alloc] init]; request.URL = @"www.fivehow.com"; test.request = request; NSData *protoBufData = [test data]; // Json Data NSString* jsonStr = @"{\"title\":\"test\", \"tag\":1, \"request\":{\"url\":\"www.fivehow.com\"},\"values\":[\"value1\", \"value2\"]}"; NSData* jsonData = [jsonStr dataUsingEncoding:(NSUTF8StringEncoding)]; NSLog(@"protoBufData: %ld", protobufdata.length); // 43 NSLog(@"jsonData: %ld", jsonData.length); / / 92Copy the code