‘CGFloat’ is not convertible to ‘UInt8#39; and other CGFloat issues with Swift and Xcode 6 beta 4(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 似乎缺乏 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)
这篇关于'CGFloat' 不能转换为 'UInt8' 和 Swift 和 Xcode 6 beta 4 的其他 CGFloat 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:'CGFloat' 不能转换为 'UInt8' 和 Swift 和
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01