ZBLOG

struct Line { let from: CGPoint let to: CGPoint } struct PoseOverlay { let dots: [CGPoint] let lines: [Line] let count:Float } private func drawDots(_ dots: [CGPoint]) { for dot in dots { let dotRect = CGRect( x: CGF...

在你提供的代码中,调用 drawCount 的方式有一些问题,因为 poseOverlay.dots.first 返回的是一个可选类型 (CGPoint?)。当你尝试将其传递给 drawCount 方法时,由于该方法期望的参数是一个非可选类型的 CGPoint,因此会导致编译错误。

解决方案

你需要确保在调用 drawCount 时,将 poseOverlay.dots.first 解包成一个非可选类型。如果 dots 数组不为空,你可以安全地解包第一个点;否则,你可以选择不绘制任何内容或提供一个默认值。

以下是修改后的代码示例:

override func draw(_ rect: CGRect) {
    for poseOverlay in poseOverlays {
        drawLines(poseOverlay.lines)
        drawDots(poseOverlay.dots)

        // 确保 dots 不为空,并且解包第一个元素
        if let firstDot = poseOverlay.dots.first {
            drawCount(poseOverlay.count, at: firstDot)
        }
    }
}

解释

  • 使用了 if let 语句来安全地解包 poseOverlay.dots.first。如果数组为空,则不会调用 drawCount 方法。
  • 如果有点存在,程序将调用 drawCount(poseOverlay.count, at: firstDot) 来绘制计数。

这样,可以避免运行时崩溃并确保你的代码更加健壮!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=21538

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?