iOS ProtocolBuffer环境配置与使用

Protocol Buffer是Google出品的一种轻量高效的结构化数据存储格式

AppStore:记录Box

Swift

一、环境配置

可以直接使用brew install swift-protobuf安装

也可以按如下步骤配置:

1.Clone Protobuf

https://github.com/apple/swift-protobuf

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

2.选择tag并构建protoc插件

// 列出所有tag
$ git tag -l
// 选择某个tag,我是选择最新的
$ git checkout tags/[tag_name]
// 构建
$ swift build -c release

构建完成后会在swift-protobuf/.build/release路径下生成protoc-gen-swift

然后复制protoc-gen-swift到环境变量路径中

二、创建.proto文件,定义结构

Protobuf3 语法指南

// 使用PB3的语法
syntax = "proto3";

// message代表一个模型
message Test {
  string title = 1;
  int32 tag = 2;
  Request request = 3;
  repeated string values = 4; // 数组
}

message Request {
  string url = 1;
}

三、把.proto文件转为Swift模型

  1. 创建两个文件夹origin_pathtarget_path,并放在同一路径Test
    (注:文件夹名自定义)

  2. 把上一步创建好的.proto放到origin_path路径下

image

cd到Test,终端执行:(注意对应路径和文件名)

protoc --proto_path=origin_path --swift_out=target_path origin_path/Test.proto
  1. target_path中查看转好的文件
image

四、在iOS工程中使用

  1. pod添加
pod 'SwiftProtobuf'
  1. 把之前转好的模型文件加入工程

  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

Objective-C

一、环境配置

1.下载Protobuf并解压

https://github.com/protocolbuffers/protobuf/releases

cd到解压好的路径下

2.终端按顺序执行

brew install autoconf automake libtool curl

./autogen.sh

./configure

make

make check

make install

3.检查是否安装成功

protoc --version

二、创建.proto文件,定义结构

Protobuf3 语法指南

// 使用PB3的语法
syntax = "proto3";

// 模型文件前缀
option objc_class_prefix = "WH";

// message代表一个模型
message Test {
  string title = 1;
  int32 tag = 2;
  Request request = 3;
  repeated string values = 4; // 数组
}

message Request {
  string url = 1;
}

三、把.proto文件转为OC模型

  1. 创建两个文件夹origin_pathtarget_path,并放在同一路径Test
    (注:文件夹名自定义)

  2. 把上一步创建好的.proto放到origin_path路径下

image

cd到Test,终端执行:(注意对应路径和文件名)

protoc --proto_path=origin_path --objc_out=target_path origin_path/Test.proto
  1. target_path中查看转好的文件
image

四、在iOS工程中使用

  1. pod添加
pod 'Protobuf'
  1. 把之前转好的模型文件加入工程

  2. 导入模型头文件

#import "Test.pbobjc.h"
  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)];
    
    // 对比 data length
    
    NSLog(@"protoBufData: %ld", protoBufData.length);   // 43
    NSLog(@"jsonData: %ld", jsonData.length);           // 92

你可能感兴趣的:(iOS ProtocolBuffer环境配置与使用)