ZBLOG

已知: static func poseOverlays( fromMultiplePoseLandmarks landmarks: [[NormalizedLandmark]], inferredOnImageOfSize originalImageSize: CGSize, ovelayViewSize: CGSize, locatMinX:Int, locatMaxX:Int, locatMinY:Int, ...

在 Swift 中,当你从可选类型(Int?)解包时,需要确保它有值,并且进行适当的强制解包或提供一个默认值。根据你的错误提示,问题出现在 locatMinXlocatMaxXlocatMinY 参数上,因为这些参数期望的是 Int 类型,但你传入的是可选的 Int? 类型。

要解决这个问题,可以使用强制解包或为其提供一个默认值。以下是如何修改你的代码来处理可选类型的示例:

使用强制解包

如果你确定这些值一定不为空,可以用 ! 强制解包(但这会导致运行时崩溃,如果它们是 nil):

let poseOverlays = OverlayView.poseOverlays(
    fromMultiplePoseLandmarks: poseLandmarkerResult.landmarks,
    inferredOnImageOfSize: imageSize,
    ovelayViewSize: weakSelf.overlayView.bounds.size,
    
    locatMinX: configJsonData?.LocationList.first?.RectPoint.first?.x!,
    locatMaxX: configJsonData?.LocationList.first?.RectPoint.first?.x!,
    locatMinY: configJsonData?.LocationList.first?.RectPoint.first?.x!,
    
    imageContentMode: weakSelf.overlayView.imageContentMode,
    andOrientation: UIImage.Orientation.from(deviceOrientation: UIDevice.current.orientation))

使用默认值

更安全的方法是使用 nil 合并运算符(??),给每个可选变量设置一个默认值:

let poseOverlays = OverlayView.poseOverlays(
    fromMultiplePoseLandmarks: poseLandmarkerResult.landmarks,
    inferredOnImageOfSize: imageSize,
    ovelayViewSize: weakSelf.overlayView.bounds.size,
    
    locatMinX: configJsonData?.LocationList.first?.RectPoint.first?.x ?? 0, // 默认值为 0
    locatMaxX: configJsonData?.LocationList.first?.RectPoint.last?.x ?? 0, // 修改此行以获取最后一个 RectPoint 的 x 值作为 max X
    locatMinY: configJsonData?.LocationList.first?.RectPoint.first?.y ?? 0, // 假设你需要 y 值而不是 x
    
    imageContentMode: weakSelf.overlayView.imageContentMode,
    andOrientation: UIImage.Orientation.from(deviceOrientation: UIDevice.current.orientation))

注意:

  1. 确保数据完整性:在做强制解包时,请确认数据结构不会返回 nil,否则可能导致应用崩溃。
  2. 正确选择最大和最小位置:检查你的 JSON 数据结构,确保提取的位置符合你的需求。上述代码示例假设了一个可能的逻辑,如果有不同的需求请相应调整。
  3. 最好实践:建议总是使用 nil 合并运算符 (??) 来处理可选值,以避免运行时错误。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?