iOS开发Protobuf数据格式使用

作者: Z小新 | 来源:发表于2019-10-18 17:23 被阅读0次

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

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

相关文章

  • iOS开发Protobuf数据格式使用

    Protobuf 是由谷歌开发的一种数据格式,类似我们常见的JSON,XML。有点我目前知道的只有小,数据量非常...

  • google protobuf ios开发使用

    参考链接:google protobuf ios开发使用http://www.cnblogs.com/cocoaj...

  • Protobuf2.6集成到ios平台

    最近我们项目组把socket通信的数据格式使用google 的Protobuf,简称“PB”,具体protobuf...

  • iOS端 Protobuf 环境的安装及使用

    iOS端 Protobuf 环境的安装及使用 1、集成Protobuf工具环境。2、创建Protobuf文件并进行...

  • iOS使用protobuf开发IM

    protobuf是google开发出来的一些协议;采用了二进制进行传输,而不是文本(json,xml)聊天过程中的...

  • iOS调用webservice -- 【WJ】

    注意: 本文主要介绍iOS如何调用webservice进行开发,以及我的需求中的UI控件使用;这里使用的数据格式是...

  • 在iOS开发中使用Protobuf

    Protobuf简介(来自百度百科) protocolbuffer(以下简称PB)是google 的一种数据交换的...

  • iOS protobuf 使用

    protoc --proto_path=proto的文件路径 --objc_out=导出的oc文件路径 指定导出的...

  • iOS:使用protobuf

    Protocol Buffers规范 支持的基本类型 扩展类型 约定 使用 1、下载protobuf库[https...

  • iOS _protobuf使用

    1.下载protobuf 使用git的方式到开源社区下载protobuf,在终端种输入以下命令: 2.安装brew...

网友评论

    本文标题:iOS开发Protobuf数据格式使用

    本文链接:https://www.haomeiwen.com/subject/beqwmctx.html