【问题标题】:Fix error in Swift simd library (and missing documentation?)修复 Swift simd 库中的错误(以及缺少文档?)
【发布时间】:2018-08-26 08:52:10
【问题描述】:

我下载了 Apple 的“DemoBot”项目以尝试了解有关GameplayKit 的更多信息,但有一个错误阻止它编译。它位于似乎使用simd 库的扩展中。我做了一些关于 SIMD 的研究,作为一个新手,我可以说这些东西已经超出了我的想象。但我只想让这段代码正常工作,以便演示项目。

错误是由下面代码中的这一行引起的:

let fractionOfComponent = max(0, min(1, componentInSegment))

错误信息是:

传递给不带参数的调用的参数

谁能告诉我如何解决这个错误?

另外,我在哪里可以找到simd 库的文档?我搜索了Apple Documentation,但simd 库上似乎什么都没有。我如何在没有计算机科学学位的情况下学习如何使用该库中的 API?

import CoreGraphics
import simd

// Omitting the other extensions here

// Extend `float2` to provide a convenience method for working with pathfinding graphs.
extension float2 {
    /// Calculates the nearest point to this point on a line from `pointA` to `pointB`.
    func nearestPointOnLineSegment(lineSegment: (startPoint: float2, endPoint: float2)) -> float2 {
        // A vector from this point to the line start.
        let vectorFromStartToLine = self - lineSegment.startPoint

        // The vector that represents the line segment.
        let lineSegmentVector = lineSegment.endPoint - lineSegment.startPoint

        // The length of the line squared.
        let lineLengthSquared = distance_squared(lineSegment.startPoint, lineSegment.endPoint)

        // The amount of the vector from this point that lies along the line.
        let projectionAlongSegment = dot(vectorFromStartToLine, lineSegmentVector)

        // Component of the vector from the point that lies along the line.
        let componentInSegment = projectionAlongSegment / lineLengthSquared

        // Clamps the component between [0 - 1].
        let fractionOfComponent = max(0, min(1, componentInSegment))

        return lineSegment.startPoint + lineSegmentVector * fractionOfComponent
    }
}

更新:

感谢 giorashc 的回答,我能够通过将有问题的行替换为以下内容来修复错误:

let componentMin = Swift.min(1, componentInSegment)
let fractionOfComponent = Swift.max(0, componentMin)

但是,我仍然想为 Swift 的 simd 库(尤其是新手可以理解的)找到一个好的文档资源。如果有人知道这样的资源,那就太好了。

【问题讨论】:

标签: ios swift sprite-kit scenekit simd


【解决方案1】:

float2 已经有一个不带参数的min 方法。 (选择在 Xcode 中定义的方法有解释)

尝试使用Swift.min显式调用swift的实现

【讨论】:

  • 谢谢,成功了!我能够通过用以下内容替换有问题的行来修复错误:let componentMin = Swift.min(1, componentInSegment)let fractionOfComponent = Swift.max(0, componentMin)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 2021-11-13
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
  • 2019-08-25
相关资源
最近更新 更多