“'CGFloat'不能转换为'Double'” SWIFT中的错误(iOS) [英] "'CGFloat' is not convertible to 'Double'" error in SWIFT (iOS)
问题描述
我试图在SWIFT中将图像剪切成9个部分。我收到此错误'CGFloat'不能转换为'Double'。当我在两个变量中加上i或j时出现此错误。
Im trying to cut an image into 9 pieces in SWIFT. Im getting this error "'CGFloat' is not convertible to 'Double'". I get this error when I put "i" or "j" in the two variables.
以下是用于剪切图像的代码的一部分。
Below is part of the code used for cutting the image.
for i in 1...3
{
for j in 1...3
{
var intWidth = ( i * (sizeOfImage.width/3.0))
var fltHeight = ( j * (sizeOfImage.height/3.0))
var portion = CGRectMake(intWidth,fltHeight, sizeOfImage.width/3.0, sizeOfImage.height/3.0);
.
.
"Code goes on"
无法确定问题是什么。请帮忙。提前致谢。
Couldn't really figure out what the problem is. Please help. Thanks in advance.
-Riyazul Aboobucker
-Riyazul Aboobucker
推荐答案
在swift中没有隐式转换数字数据类型,您正在混合整数和浮点数。
您必须将索引显式转换为 CGFloat
s:
In swift there is no implicit conversion of numeric data types, and you are mixing integers and floats.
You have to explicitly convert the indexes to CGFloat
s:
var intWidth = ( CGFloat(i) * (sizeOfImage.width/3.0))
var fltHeight = ( CGFloat(j) * (sizeOfImage.height/3.0))
正如swift中经常发生的那样,错误信息具有误导性 - 它说:
As often happening in swift, the error message is misleading - it says:
'CGFloat'不能转换为'Double'
'CGFloat' is not convertible to 'Double'
而我希望:
'CGFloat'不能转换为'Int'
'CGFloat' is not convertible to 'Int'
这篇关于“'CGFloat'不能转换为'Double'” SWIFT中的错误(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!