iOS 如何使用protocol Buffers

一、Protocol buffers 是什么?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data -- think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.
Protocol buffers 是 Google 的语言中立、平台中立、可扩展的序列化结构化数据机制 - 类似于 XML,但更小、更快、更简单。您只需定义一次数据的结构化方式,然后就可以使用特殊生成的源代码,使用各种语言轻松地在各种数据流中写入和读取结构化数据。

优点是更小更快;且对于数据扩展兼容性好,因为新增数据字段不影响旧字段解析;语义清晰且学习门槛低;

如果想详细了解可戳这:protobuf.dev

二、怎么使用

这里先介绍如何将.proto文件转换成.pbobjc.h和.pbobjc.m文件,然后导入到项目中,并实现pbData转pbModel,pbModel转pbData

Protocol buffers 开源并托管在Github,详见https://github.com/google/protobuf/
google提供了pb转oc的方法,但是没有提供swift的,apple这里做了swift的使用,详见https://github.com/apple/swift-protobuf

切记一定要安装3.0以上的版本,才支持OC,而且功能更好用;

推荐使用Homebrew来安装:

c 复制代码
brew install automake 
brew install libtool 
brew install protobuf

安装后check一下,可输出当前安装版本

c 复制代码
protoc --version

准备好这些后,

  1. 集成集成protobuf库
c 复制代码
 pod 'Protobuf'
  1. 将.proto文件转换成.pbobjc.h和.pbobjc.m文件

先cd到.proto文件所在目录,然后执行如下操作,会在同级目录里生成.pbobjc.h和.pbobjc.m文件;

c 复制代码
protoc --proto_path=. --objc_out=. xxxx.proto

注意: 这里生成的.pbobjc.h和.pbobjc.m文件无法直接导入项目中使用,你会发现文件中一堆MRC的代码;

参考如下:iOS 组件中设置文件支持MRC

至此,项目build是可以success的,接下来是如何使用

  1. pbModel转pbData

pb提供了一个data方法,可将使用上面步骤导入项目的pbobjc类也可以理解成model类转换成data

c 复制代码
/**
 * Serializes the message to an NSData.
 *
 * If there is an error while generating the data, nil is returned.
 *
 * @note This value is not cached, so if you are using it repeatedly, cache
 *       it yourself.
 *
 * @note In DEBUG ONLY, the message is also checked for all required field,
 *       if one is missing, nil will be returned.
 *
 * @return The binary representation of the message.
 **/
- (nullable NSData *)data;

如下示例,xxpbModel就是代表一个pbobjc类

c 复制代码
NSData *pbData = [xxpbModel data];
  1. pbData转pbModel

pb提供了一个init方法来将一个data映射成pbobjc类

c 复制代码
/**
 * Initializes an instance by parsing the data. This method should be sent to
 * the generated message class that the data should be interpreted as. If
 * there is an error the method returns nil and the error is returned in
 * errorPtr (when provided).
 *
 * @note In DEBUG builds, the parsed message is checked to be sure all required
 *       fields were provided, and the parse will fail if some are missing.
 *
 * @note The errors returned are likely coming from the domain and codes listed
 *       at the top of this file and GPBCodedInputStream.h.
 *
 * @param data     The data to parse.
 * @param errorPtr An optional error pointer to fill in with a failure reason if
 *                 the data can not be parsed.
 *
 * @return An initialized instance of the generated class.
 **/
- (nullable instancetype)initWithData:(NSData *)data 
                                error:(NSError **)errorPtr;

如下示例,生成一个pbobjc类对象或者说一个pbModel

XXPbModel 是一个pbobjc类名

c 复制代码
NSError *error;
XXPbModel *pbModel = [[XXPbModel alloc] initWithData:data error:&error];
相关推荐
文件夹__iOS3 小时前
AsyncStream 进阶实战:SwiftUI 全局消息流极简实现
ios·swiftui·swift
2501_916008895 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
忆江南5 小时前
Flutter深度全解析
ios
山水域5 小时前
Swift 6 严格并发检查:@Sendable 与 Actor 隔离的深度解析
ios
楚轩努力变强6 小时前
iOS 自动化环境配置指南 (Appium + WebDriverAgent)
javascript·学习·macos·ios·appium·自动化
游戏开发爱好者81 天前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
黑码哥1 天前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
2501_915106321 天前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
熊猫钓鱼>_>1 天前
移动端开发技术选型报告:三足鼎立时代的开发者指南(2026年2月)
android·人工智能·ios·app·鸿蒙·cpu·移动端