swift使用swift-protobuf协议通讯,使用指北

什么是Protobuf

Protobuf(Protocol Buffers)协议😉 Protobuf 是一种由 Google 开发的二进制序列化格式和相关的技术,它用于高效地序列化和反序列化结构化数据,通常用于网络通信、数据存储等场景。

为什么要使用Protobuf? 因为快

其实 Protobuf 在许多领域都得到了广泛应用,特别是在分布式系统、RPC(Remote Procedure Call)框架和数据存储中,它提供了一种高效、简洁和可扩展的方式来序列化和交换数据,

Protobuf 的主要优点包括:

高效性:Protobuf 序列化后的二进制数据通常比其他序列化格式(比如超级常用的JSON)更小,并且序列化和反序列化的速度更快,这对于性能敏感的应用非常有益。
简洁性:Protobuf 使用一种定义消息格式的语法,它允许定义字段类型、顺序和规则(消息结构更加清晰和简洁)
版本兼容性:Protobuf 支持向前和向后兼容的版本控制,使得在消息格式发生变化时可以更容易地处理不同版本的通信。
语言无关性:Protobuf 定义的消息格式可以在多种编程语言中使用,这有助于跨语言的通信和数据交换(截至本文发布目前官方支持的有C++/C#/Dart/Go/Java/Kotlin/python/Swift)
自动生成代码:Protobuf 通常与相应的工具一起使用,可以自动生成代码,包括序列化/反序列化代码和相关的类(减少了手动编写代码的工作量,提高效率)

安装编译器

注意:需要swift5.8以上的版本和xcode14.3以后的版本才可以

你也可以通过命令行使用 swift --version 命令来查看安装的Swift版本。这将会输出类似于下面的信息,显示当前安装的Swift编译器的版本:

安装swift-protobuf:这条命令将会给你的电脑上安装 protoc 和 Swift 代码生成器插件。

brew install swift-protobuf

根据官方文档可以使用brew安装:

安装完之后,输入protoc将会出现提示:

编译Proto文件

将.proto文件编译为swift代码:--swift_out=. 表示将生成的文件放在当前文件夹中

protoc --swift_out=. my.proto

运行之后,就会出现一个GameMsg.pb.swift文件,这就是编译后的swift文件:

生成的代码将为每个 proto 字段公开一个 Swift 属性以及一组序列化和反序列化功能。假如你的proto文件内容是:

syntax = "proto3";

message BookInfo {
   int64 id = 1;
   string title = 2;
   string author = 3;
}

那么生成的swift代码:

// DO NOT EDIT.
// swift-format-ignore-file
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: game.proto
//
// For information on using the generated types, please see the documentation:
//   https://github.com/apple/swift-protobuf/

import Foundation
import SwiftProtobuf

// If the compiler emits an error on this type, it is because this file
// was generated by a version of the `protoc` Swift plug-in that is
// incompatible with the version of SwiftProtobuf to which you are linking.
// Please ensure that you are building against the same version of the API
// that was used to generate this file.
fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {
  struct _2: SwiftProtobuf.ProtobufAPIVersion_2 {}
  typealias Version = _2
}

struct BookInfo {
  // SwiftProtobuf.Message conformance is added in an extension below. See the
  // `Message` and `Message+*Additions` files in the SwiftProtobuf library for
  // methods supported on all messages.

  var id: Int64 = 0

  var title: String = String()

  var author: String = String()

  var unknownFields = SwiftProtobuf.UnknownStorage()

  init() {}
}

#if swift(>=5.5) && canImport(_Concurrency)
extension BookInfo: @unchecked Sendable {}
#endif  // swift(>=5.5) && canImport(_Concurrency)

// MARK: - Code below here is support for the SwiftProtobuf runtime.

extension BookInfo: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
  static let protoMessageName: String = "BookInfo"
  static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
    1: .same(proto: "id"),
    2: .same(proto: "title"),
    3: .same(proto: "author"),
  ]

  mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
    while let fieldNumber = try decoder.nextFieldNumber() {
      // The use of inline closures is to circumvent an issue where the compiler
      // allocates stack space for every case branch when no optimizations are
      // enabled. https://github.com/apple/swift-protobuf/issues/1034
      switch fieldNumber {
      case 1: try { try decoder.decodeSingularInt64Field(value: &self.id) }()
      case 2: try { try decoder.decodeSingularStringField(value: &self.title) }()
      case 3: try { try decoder.decodeSingularStringField(value: &self.author) }()
      default: break
      }
    }
  }

  func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
    if self.id != 0 {
      try visitor.visitSingularInt64Field(value: self.id, fieldNumber: 1)
    }
    if !self.title.isEmpty {
      try visitor.visitSingularStringField(value: self.title, fieldNumber: 2)
    }
    if !self.author.isEmpty {
      try visitor.visitSingularStringField(value: self.author, fieldNumber: 3)
    }
    try unknownFields.traverse(visitor: &visitor)
  }

  static func ==(lhs: BookInfo, rhs: BookInfo) -> Bool {
    if lhs.id != rhs.id {return false}
    if lhs.title != rhs.title {return false}
    if lhs.author != rhs.author {return false}
    if lhs.unknownFields != rhs.unknownFields {return false}
    return true
  }
}

在项目中使用的话:

// 创建一个BookInfo对象,并给它赋值
var info = BookInfo()
info.id = 1734
info.title = "Really Interesting Book"
info.author = "Jane Smith"

// 如上所述,但生成只读值:
let info2 = BookInfo.with {
    $0.id = 1735
    $0.title = "Even More Interesting"
    $0.author = "Jane Q. Smith"
  }

// 序列化为二进制protobuf格式:可以选择序列化为
// 符合SwiftProtobufContiqueBytes的任何类型。例如
let binaryData: Data = try info.serializedBytes()
let binaryDataAsBytes: [UInt8] = try info.serializedBytes()


// 从`binaryData反序列化接收到的Data对象`
let decodedInfo = try BookInfo(serializedData: binaryData)

// 反序列化从`binaryDataAsBytes接收的[UInt8]对象`
let decodedInfo = try BookInfo(serializedBytes: binaryDataAsBytes)

//序列化为JSON格式的数据对象,或符合
//SwiftProtobufContiqueBytes。例如
let jsonData: Data = try info.jsonUTF8Data()
let jsonBytes: [UInt8] = try info.jsonUTF8Bytes()

// 从`jsonBytes从JSON格式反序列化`
let receivedFromJSON = try BookInfo(jsonUTF8Bytes: jsonBytes)

添加SwiftProtobuf到你的项目中

要想使用Protobuf还需要在你的项目中添加SwiftProtobuf,可以使用SPM添加或者使用CocoaPods添加,我这里习惯使用SPM,在xcode项目中可以直接右键添加包就可以了:

添加之后就可以在左侧项目的依赖包里面显示出来了:

解决遇到的问题

1.如果你在finder中在proto文件夹中生成swift代码,但是当你会到xcode总,发现proto文件夹中不一定会显示proto文件和生成的swift文件:

需要在xcode中手动添加文件到文件夹中:

再回到项目文件夹就可以看到了:

2.但是这个时候,当你打开生成的swift文件,可能发现报错了:

Could not find module 'SwiftProtobuf' for target 'arm64-apple-watchos'; found: arm64_32-apple-watchos, at: /Users/song/Library/Developer/Xcode/DerivedData/mywatch-coaeflxmpfflredurreogfyfvbxz/Index.noindex/Build/Products/Debug-watchos/SwiftProtobuf.swiftmodule 

这是因为你没有将 SwiftProtobuf 真正添加到项目中:

需要你手动再将依赖添加到项目中

就不会报错了:

简单的DEMO

我写了一个简单的Demo:

//
//  ContentView.swift
//  mywatch Watch App
//
//  Created by song on 2024/6/19.
//

import SwiftUI

struct ContentView: View {
    // Create a BookInfo object and populate it:
    @State var info = BookInfo()
    // binaryData
    @State var binaryData: Data = .init()
    // binaryInfo
    @State var binaryInfo: BookInfo = .init()

    var body: some View {
        VStack {
            Text(info.author)
            Text("Hello, 1024小神!")
            Button(action: {
                info.id = 1734
                info.title = "Really Interesting Book"
                info.author = "Jane Smith"
            }, label: {
                Text("编辑")
            })
            Button(action: {
                binaryData = try! info.serializedData()
                print("二进制数据:\(binaryData)")
            }, label: {
                Text("序列化")
            })
            Button(action: {
                binaryInfo = try! BookInfo(serializedData: binaryData)
                print("反序列化数据:\(binaryInfo)")
            }, label: {
                Text("反序列化")
            })
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

 可以实现协议消息的序列化和反序列化:

  • 42
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用以下命令安装 google-protobuf: npm install --save google-protobuf 这个命令会将 google-protobuf 模块安装到你的项目中,并将其保存为依赖项。 然后,你可以使用以下路径引入 empty_pb.js 模块: google-protobuf/google/protobuf/empty_pb.js 这将允许你在代码中使用 empty_pb.js 模块。请确保在你的代码中正确引入这个路径。 需要注意的是,确保你已经在安装 google-protobuf 之前完成了 protobufjs 的安装,以便确保这两个模块之间的依赖关系正确。如果你遇到了安装问题或错误提示,你可以使用 --force 选项来强制执行安装命令,以解决已存在的文件问题。 希望这些信息对你有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [protobuf.js的使用](https://blog.csdn.net/qq_27868061/article/details/114038738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [npm install -g protobufjs](https://blog.csdn.net/qifenzhisanjin/article/details/124952503)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024小神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值