在swift中使用CGFloats进行数学运算的最佳方法是什么?

时间:2021-07-14 02:59:00

I tried to do some simple UIView layout math in swift and tried the following line of code...

我尝试在swift中做一些简单的UIView布局数学,并尝试了以下代码行...

var offset: CGFloat = (bounds.width / 2.0) - ((sortedSymptoms.count * bounds.height) / 2.0)

and got the following error from the compiler:

并从编译器得到以下错误:

cannot invoke '-' with an argument list of type '(($T6), ($T17))'
        var offset: CGFloat = (bounds.width / 2.0) - ((sortedSymptoms.count * bounds.height) / 2.0)
                              ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    

The compiler error isn't all that helpful, but it looks like there's a type conflict between Double, Int, and CGFloat of some kind. I was able to get the line to compile by sprinkling in some explicit creations of CGFloats, but I can't believe that this is the right way to do this.

编译器错误并没有那么有用,但看起来某种类型的Double,Int和CGFloat之间存在类型冲突。我能够通过在CGFloats的一些明确创作中进行编译来获得编译,但我无法相信这是正确的方法。

var offset: CGFloat = (bounds.width / CGFloat(2.0)) - ((CGFloat(sortedSymptoms.count) * bounds.height) / CGFloat(2.0))

What's the right way?

什么是正确的方法?

1 个解决方案

#1


7  

This is a known issue in Swift and the dev team has been working on improving the issue around CGFloat in particular. But at this time, yes, that's how you write it.

这是Swift中的一个已知问题,开发团队一直致力于改善CGFloat周围的问题。但是在这个时候,是的,这就是你写它的方式。

Some followup from devforums (which may make you happy or sad, but at least roughly explains the current status): https://devforums.apple.com/message/1026028#1026028

来自devforums的一些后续(可能会让你开心或悲伤,但至少大致解释当前状态):https://devforums.apple.com/message/1026028#1026028

Note that the main issue here is that the literal 2.0 doesn't coerce to CGFloat, which it arguably should. But count will likely always require a cast, by intent. You cannot always safely convert between numeric types, and Swift intentionally forces you to consider each time you do these kinds of casts. But it should be possible to determine if a literal conversion is safe at compile-time, so that should be fixable.

请注意,这里的主要问题是文字2.0不会强制CGFloat,它可以说是应该的。但是计数可能总是需要投射,意图。你不能总是安全地在数字类型之间进行转换,而Swift故意强迫你在每次进行这些类型的转换时都要考虑。但是应该可以确定文字转换在编译时是否安全,因此应该是可以修复的。

#1


7  

This is a known issue in Swift and the dev team has been working on improving the issue around CGFloat in particular. But at this time, yes, that's how you write it.

这是Swift中的一个已知问题,开发团队一直致力于改善CGFloat周围的问题。但是在这个时候,是的,这就是你写它的方式。

Some followup from devforums (which may make you happy or sad, but at least roughly explains the current status): https://devforums.apple.com/message/1026028#1026028

来自devforums的一些后续(可能会让你开心或悲伤,但至少大致解释当前状态):https://devforums.apple.com/message/1026028#1026028

Note that the main issue here is that the literal 2.0 doesn't coerce to CGFloat, which it arguably should. But count will likely always require a cast, by intent. You cannot always safely convert between numeric types, and Swift intentionally forces you to consider each time you do these kinds of casts. But it should be possible to determine if a literal conversion is safe at compile-time, so that should be fixable.

请注意,这里的主要问题是文字2.0不会强制CGFloat,它可以说是应该的。但是计数可能总是需要投射,意图。你不能总是安全地在数字类型之间进行转换,而Swift故意强迫你在每次进行这些类型的转换时都要考虑。但是应该可以确定文字转换在编译时是否安全,因此应该是可以修复的。