'CGFloat' 不能转换为 'UInt8' 和 Swift 和

时间:2023-02-14
本文介绍了'CGFloat' 不能转换为 'UInt8' 和 Swift 和 Xcode 6 beta 4 的其他 CGFloat 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这能说明问题,这里是原始的 Objective-C 代码.

In case this illuminates the problem, here's the original Objective-C code.

int x = (arc4random()%(int)(self.gameView.bounds.size.width*5)) - (int)self.gameView.bounds.size.width*2;
int y = self.gameView.bounds.size.height;
drop.center = CGPointMake(x, -y);

我从这段代码开始.第 2 行和第 3 行很好,为了清楚起见,我稍后再展示它们.

I started out with this code. Lines 2 and 3 are fine, I'm presenting them for clarity later.

let x = CGFloat(arc4random_uniform(UInt32(self.gameView.bounds.size.width * 5))) - self.gameView.bounds.size.width * 2
let y = self.gameView.bounds.size.height
dropView.center = CGPointMake(x, -y)

在 Xcode 6 beta 3 中,必须将 arc4random_uniform UInt32 结果转换为 CGFloat 才能使减法和乘法起作用.这不再起作用,编译器显示错误:

In Xcode 6 beta 3, it was necessary to cast the arc4random_uniform UInt32 result to CGFloat in order for the minus and multiplication to work. This doesn't work anymore and the compiler shows an error:

‘CGFloat’不能转换为‘UInt8’

‘CGFloat’ is not convertible to ‘UInt8’

发行说明状态:

CGFloat 现在是一种独特的浮点类型,它在 32 位架构上封装了 Float 或在 64 位架构上封装了 Double.它提供了 Float 和 Double 的所有相同比较和算术运算,并且可以创建使用数字文字.使用 CGFloat 将您的代码与您的代码可能出现的情况隔离开来! 适用于 32 位,但在构建 64 位时失败,反之亦然.(17224725)"

"CGFloat is now a distinct floating-point type that wraps either a Float on 32-bit architectures or a Double on 64-bit architectures. It provide all of the same comparison and arithmetic operations of Float and Double and may be created using numeric literals. Using CGFloat insulates your code from situations where your code would be !fine for 32-bit but fail when building for 64-bit or vice versa. (17224725)"

我只是在类型上做错了吗?我什至不知道如何更好地描述这个问题,以便向 Apple 提交 beta 4 的错误报告.我拥有的几乎每个 Swift 项目都受到了这个问题的影响,所以我寻找一些理智.

Am I just doing something wrong with types? I don't even know how to describe this problem better to submit a bug report to Apple for beta 4. Pretty much every single Swift project I have that does any kind of point or rect manipulation got hit by this issue, so I'm looking for some sanity.

推荐答案

由于 Swift 没有隐式类型转换,您必须指定所有发生的类型转换.让这个案例特别乏味的是,目前 Swift 似乎缺乏 CGFloatUInt32 等类型之间的直接转换,你必须像你一样通过一个中间类型发现了.

Since Swift doesn't have implicit type conversions, you must specify all the type conversions that take place. What makes this case particularly tedious, is that currently Swift seems to lack direct conversions between CGFloat and types such as UInt32, and you must go through an intermediate type as you've discovered.

最后,arc4random_uniform需要进行两次双重转换:

In the end, two double conversions are needed for the arc4random_uniform:

let bounds = CGRectMake(0.0, 0.0, 500.0, 500.0)
var x = CGFloat(UInt(arc4random_uniform(UInt32(UInt(bounds.size.width) * 5))))
x -= bounds.size.width * 2
let center = CGPointMake(x, -bounds.size.height)

这篇关于'CGFloat' 不能转换为 'UInt8' 和 Swift 和 Xcode 6 beta 4 的其他 CGFloat 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:检查对象是否为类类型 下一篇:如何通过选择器方法传递 int 值?

相关文章

最新文章