快速上手 iOS Protocol Buffer

快速上手 iOS Protocol Buffer | 来自缤纷多彩的灰

本文主要介绍在 iOS 开发中如何快速上手使用 Protobuf 。更多关于 Protobuf 的介绍和相关的功能 api,读者可自行查阅官网

Protocol Buffer(简称 Protobuf)是一种由Google开发的语言中立、平台无关的序列化数据结构的方法。它允许你定义结构化的数据,并提供一种高效且灵活的方式进行数据序列化和反序列化。

安装 Protobuf 工具

最简单的方式是直接通过 brew 进行安装:

复制代码
brew install protobuf           // 支持生成.h和.m文件,和其他多种语言的文件
brew install swift-protobuf     // 支持生成.swift文件

检查是否安装成功:

protoc --version

protoc-gen-swift --version

创建 .proto 文件

// 使用V3语法

syntax = "proto3"

// OC语言可选,添加模型文件前缀

option objc_class_prefix = "MY"

// message代表一个模型

message Test {

string title = 1;

int32 tag = 2;

Request request = 3; // 自定义的 Request 类型

repeated string values = 4; // 数组

}

message Request {

string url = 1;

}

OC、Swift 代码生成

Protobuf 提供api用于根据.proto文件生成代码,需传入两个参数,生成结果与参数的传入顺序无关:

  1. .proto 文件的路径(下文中用 source_path 表示)
  2. 需要生成的目标语言 (下文用 target_language 表示)以及文件的输出路径(下文用 target_path 表示)

protoc source_path/xxx.proto --target_language_out=target_path

protoc --objc_out=. xxx.proto // 在当前文件夹根据xxx.proto生成.h和.m文件

protoc xxx.proto --swift_out=. // 在当前文件夹根据xxx.proto生成.swift文件

在 iOS 工程中的使用

1.工程添加依赖

pod 'Protobuf' // OC和其他多种语言的能力依赖

pod 'SwiftProtobuf' // swift能力依赖

2.把转换后的代码文件加入到工程。

3.跟正常使用某个类的方法一样。

复制代码
/*
 {
     "title": "test",
     "tag": 1,
     "request": {
         "url": "www.fivehow.com"
     },
     "values": ["value1", "value2"]
 }
 */

let request = Request.with { $0.url = "www.whlcj.github.io" }
// ProtoBuf data
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)
// Json Data
let jsonStr = "{\"title\":\"test\", \"tag\":1, \"request\":{\"url\":\"www.whlcj.github.io\"},\"values\":[\"value1\", \"value2\"]}"

let jsonStrData = jsonStr.data(using: .utf8)

// 对比 data length
print("binaryData: \(binaryData.count)")            // 43

guard let jsonStrData = jsonStrData else { return }
print("jsonStrData: \(jsonStrData.count)")          // 92

protobuf 基础类型与Swift类型映射关系

Proto type Swift Type
int32 Int32
sint32 Int32
sfixed32 Int32
uint32 UInt32
fixed32 UInt32
int64 Int64
sint64 Int64
sfixed64 Int64
uint64 UInt64
fixed64 UInt64
bool Bool
float Float
double Double
string String
bytes Data
相关推荐
Cutecat_3 小时前
视频字幕处理工具横向:提取模式 vs 编辑模式,该如何选择
android·前端·ios·语音识别
2601_961845427 小时前
法考真题及答案解析|历年真题|资料已整理
linux·windows·ubuntu·macos·centos·gnu
大熊猫侯佩8 小时前
WWDC26 SwiftUI 进化之路:砸碎黑盒,彻底迎来开发自由!
ios·swiftui·swift
游戏开发爱好者89 小时前
iPhone真机调试有哪些方法?一次定位推送权限问题时整理出来的几种方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
Allen Su10 小时前
【Mac 教程系列第 20 篇】macOS 鼠须管(Squirrel)皮肤大全(持续更新)
macos·rime·squirrel·rime 输入法皮肤大全
LinMin_Rik13 小时前
Mac上获取私钥证书P12文件(也可以给win11的HbuilderX使用)
macos
音视频牛哥15 小时前
macOS如何实现RTSP/RTMP低延迟播放? SmartMacPlayer技术实战探究
macos·大牛直播sdk·mac rtsp播放器·mac rtmp·mac rtmp播放器·mac平台播放rtsp·mac平台播放rtmp
大熊猫侯佩16 小时前
WWDC26 最被忽视的王炸:告别“伪并发”陷阱,Swift 6.4 的 async defer
ios·swift·编程语言
一杯奶茶¥16 小时前
苹果系统可引导镜像 macOS 原版可引导镜像
macos
BugShare17 小时前
Mac 上原生开发的开源免费、尽享丝滑数据库工具
数据库·macos·开源