简体   繁体   English

在Swift中返回布尔值时收到错误消息

[英]Getting error message when returning a boolean in Swift

I have this function: 我有这个功能:

func isDrawRadiusEnough(let point: CGPoint) -> Bool {
    let distance = sqrtf(powf(point.x-lastPointDrawn.x, 2)+powf(point.y-lastPointDrawn.y, 2))

    return distance > drawRadius // Error: Could not find an overload for '>' that accepts the supplied arguments
}

drawRadius is an Int and is a property in the same class as the function. drawRadius是一个Int并且是与该函数相同的类中的一个属性。 lastPointDrawn is a CGPoint and is also a property. lastPointDrawnCGPoint ,也是一个属性。

When I set the drawRadius as a Float the error disappears, but I don't want it to be a float. 当我将drawRadius设置为Float时,错误消失了,但是我不希望它是Float。 In Obj-C this was valid but it is not in Swift. 在Obj-C中,这是有效的,但在Swift中则无效。 Anyone know a workaround? 有人知道解决方法吗?

sqrtf returns a Float value. sqrtf返回一个Float值。

Since drawRadius is an Int , you need to convert it to a Float prior to performing the compare operation. 由于drawRadiusInt ,因此需要在执行比较操作之前将其转换为Float

Try return distance > Float(drawRadius) 尝试return distance > Float(drawRadius)

This is due to Swift's implementation of type safety. 这是由于Swift实施了类型安全。 No implicit type conversions are performed. 不执行隐式类型转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM