ios - Can 8 bytes be NIL?
问题描述
I am trying to build a BLE packet that contains a RGB color and !C to ID the packet type, using the built-in color picker and some math it works well to get the RGB values and converting the Int to Data seems to work fine but it throws a Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
var s1 = lroundf(Float(Character("!").asciiValue!))
var s2 = lroundf(Float(Character("C").asciiValue!))
var rint = lroundf(Float(r*255))
var gint = lroundf(Float(g*255))
var bint = lroundf(Float(b*255))
let s1data = Data(bytes: &s1, count: MemoryLayout.size(ofValue: s1))
let s2data = Data(bytes: &s2, count: MemoryLayout.size(ofValue: s2))
let rdata = Data(bytes: &rint, count: MemoryLayout.size(ofValue: rint))
let gdata = Data(bytes: &gint, count: MemoryLayout.size(ofValue: gint))
let bdata = Data(bytes: &bint, count: MemoryLayout.size(ofValue: bint))
print("The data going into this packet is, s1: \(s1data), s2: \(s2data), rdata: \(rdata), gdata: \(gdata), bdata: \(bdata)")
packet.append(s1data)
packet.append(s2data)
packet.append(rdata)
packet.append(gdata)
packet.append(bdata)
return packet
The output from this segment is:
The data going into this packet is, s1: 8 bytes, s2: 8 bytes, rdata: 8 bytes, gdata: 8 bytes, bdata: 8 bytes
DQ_Plate/ViewController.swift:87: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
2021-11-09 13:33:03.509765-0600 DQ Plate[567:20901] DQ_Plate/ViewController.swift:87: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
It fails on the line with packet.append(s1data)
but as you can see from the output it has 8 bytes
Not sure what is happening here…</p>
解决方案
推荐阅读
- reactjs - 传递路径参数在 URL 调用中返回 object%20Object
- db2 - 使用 PowerHA 的 DS8K 存储配置类型
- database - 在同一个数据库中安装 wordpress 和 laravel,因为我的主机不允许多个数据库
- javascript - 如何在jquery中获取最近的选定下拉值?
- asp.net-core - OHIF 查看器不显示来自我的 .net 核心服务器的 DICOM 图像
- node.js - 当主入口点移动到子文件夹时,Sails (1.2.3) 给出纯文本“未找到”(404),我该如何解决?
- docker - 复制文件 - 在 Dockerfile 旁边 - 不起作用并阻止 docker build
- monitoring - 用于 Tibco BW6 的 Hawk 监控
- xamarin - Visual Studio 2019 Xamarin Forms android 模拟器失败并显示消息系统 UI 没有响应
- python - 从目录中为 CNN 加载 Keras 中的图像,但在 CSV 文件中添加标签