iOS app打包上线测试流程优秀第三方框架学习

iOS开发Protobuf数据格式使用

2019-10-18  本文已影响0人  Z小新

Protobuf 是由谷歌开发的一种数据格式,类似我们常见的JSON,XML。有点我目前知道的只有小,数据量非常小。因项目需求接触到。下面开始正经的

1、中的输入 brew -v 检查是否有安装
2、(安装跳过) 安装brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
3、安装所需的库 一个一个运行

  brew install automake
  brew install libtool
  brew install protobuf

4、google/protobuf 地址

下载完成后 安装方式在这 就是运行下载好的full_mac_build.sh
直接找到文件 拖到终端 回车 就开始安装了

5、将Protobuf 数据模型转成 OC 文件 ,下载回来的工程会有个示例

image.png

我们要做的就是将它转成OC模型

建一个文件夹 address.proto文件拷贝进去

终端运行

protoc ./addressbook.proto --objc_out=./

查看输出结果


image.png

将文件拉入工程 ,编译,这时会报错,应为开了ARC。
设置文件关闭ARC


image.png

好了到这一步我们就可以使用了 ,与OC的模型没有差异

Person *p = [Person new];
p.name = @"张三";
p.email = @"ssss@aaaaa";

// 序列化
NSData *data = [p data];

// 反序列化
Person *p2=[Person parseFromData:data error:nil];
NSLog(@"p2%@",p2);

到此为止我们就已完成使用,接下来说一下 怎么编写自己的Protobuf文件,我们来解读一下示例中的

// See README.txt for information and build instructions.
//
// Note: START and END tags are used in comments to define sections used in
// tutorials.  They are not part of the syntax for Protocol Buffers.
//
// To get an in-depth walkthrough of this file and the related examples, see:
// https://developers.google.com/protocol-buffers/docs/tutorials

// [START declaration]
syntax = "proto3";  //  软件版本号
package tutorial;   //  包名,自定义

import "google/protobuf/timestamp.proto";// 引用库,(我感觉用不到)
// [END declaration]

// [START java_declaration]
option java_package = "com.example.tutorial";  // 地址 名称 这些  自定义都不需要
option java_outer_classname = "AddressBookProtos";
// [END java_declaration]

// [START csharp_declaration]
option csharp_namespace = "Google.Protobuf.Examples.AddressBook";
// [END csharp_declaration]

// [START messages]
message Person { // 类名

  string name = 1; // 属性名称    序号   序号不能重复
  int32 id = 2;  // Unique ID number for this person.
  string email = 3;

  enum PhoneType { // 枚举
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber { // 模型中的模型
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;// 模型数组

  google.protobuf.Timestamp last_updated = 5; // 更新时间字段
}


// Our address book file is just one of these.
message AddressBook { //  AddressBook 模型
  repeated Person people = 1;
}
// [END messages]

以上是示例模型,下面看一下自定义的模型。
// See README.txt for information and build instructions.
//
// Note: START and END tags are used in comments to define sections used in
// tutorials. They are not part of the syntax for Protocol Buffers.
//
// To get an in-depth walkthrough of this file and the related examples, see:
// https://developers.google.com/protocol-buffers/docs/tutorials

// [START declaration]
syntax = "proto3";
package zhouseUserLogModel;

// [START messages]
message logModel {

    string name = 1;
    string userId = 2;

    message log {
        string logId = 1;
        string userId = 2;  // Unique ID number for this person
        string deviceType = 3;
        string version = 4;
        string channel = 5;  // Unique ID number for this person.
        string language = 6;
        string passportId = 7;
        string companyId = 8;  // Unique ID number for this person.
        string createTime = 9;
        string pageUrl = 10;
        string action = 11;  // Unique ID number for this person.
        string clickId = 12;
        string clickName = 13;
    }

    repeated log logs = 3;
}

我将PB格式的文件,与JSON文件写入本地,对比了一下大小。


image.png

以上为,全部内容,学习交流,有不对的地方请指出。

上一篇下一篇

猜你喜欢

热点阅读