Swift和Xcode 6 beta 4无法将"CGFloat"转换为"UInt8"和其他CGFloat问题 [英] ‘CGFloat’ is not convertible to ‘UInt8' and other CGFloat issues with Swift and Xcode 6 beta 4

查看:107
本文介绍了Swift和Xcode 6 beta 4无法将"CGFloat"转换为"UInt8"和其他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位使用!fine,但对于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的错误报告.我拥有的几乎每一个执行任何类型的点操作或rect操作的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似乎缺少CGFloat和诸如UInt32之类的类型之间的直接转换,并且您必须经过发现的中间类型.

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)

这篇关于Swift和Xcode 6 beta 4无法将"CGFloat"转换为"UInt8"和其他CGFloat问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆