嗨,我是 swift 的新手,我正在尝试解码以 [Byte] 形式发回给我的字节数组?当我尝试使用
if let string = String(bytes: d, encoding: .utf8) {
print(string)
} else {
print("not a valid UTF-8 sequence")
}
它说需要解包。我无法更改返回可选数组的基本代码,因为它是从框架返回的。
最佳答案
使用guard let
guard let unwrappedBytes = d else { return }
有了这个,您将获得与以前完全相同的数据,但没有问号。
我知道很难理解 Swift 中的可选内容。我建议查看 Apple Docs,它非常好: https://developer.apple.com/documentation/swift/optional
关于ios - 转换[字节]?到[字节],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53555369/