ios - 转换[字节]?到[字节]

标签 ios swift

嗨,我是 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/

相关文章:

ios - 删除特定的 mapView.overlay

Swift 后台数据任务

ios - 以编程方式从模态视图 Controller 切换父选项卡

ios - 取消 UIWebView loadRequest

ios - 快速将图像从设备加载到 tableView

ios - 如何围绕自定义蒙版创建描边?

ios - 无法从 Dropbox 中删除文件(从 iPhone 应用程序)

ios - 如何在iOS中获取用户所在位置的经纬度

ios - MapView 未在函数中实例化

ios - 我可以将一个标签固定在另一个标签内以创建边框吗?