简体   繁体   English

如何在Swift中将protobuf对象转换为ByteArray并使用Base64 URL_SAFE进行编码?

[英]To how convert protobuf object to ByteArray and the encode with Base64 URL_SAFE in Swift?

In Android, I could convert an object to ByteArray and then encode it to Base64 (URL_SAFE) as per code below 在Android中,我可以按照以下代码将对象转换为ByteArray,然后将其编码为Base64(URL_SAFE)

    val myByteArrayObject = protobufObject.toByteArray()
    val meEncodedObject = android.util.Base64.encodeToString.encodeToString(
          myByteArrayObject, android.util.Base64.DEFAULT).trim()

How could I achieve that in Swift? 如何在Swift中实现?

Found the answer. 找到了答案。

do {
    let protobufSerialized = try protobufObject.serializedData()
    let protobufEncoded = protobufSerialized.base64EncodedString()

    // Do whatever need to be done with the protobufEncoded
} catch {  }

The main hidden function that is hard to find is serializedData() that exist on SwiftProtobuf.Message 很难找到的主要隐藏函数是SwiftProtobuf.Message上存在的serializedData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM