简体   繁体   English

Swift(iOS)中的“'CGFloat'无法转换为'Double'”错误

[英]“'CGFloat' is not convertible to 'Double'” error in Swift (iOS)

I'm trying to cut an image into 9 pieces in Swift. 我正在尝试在Swift中将图像切成9个片段。 I'm getting this error: 我收到此错误:

'CGFloat' is not convertible to 'Double' “ CGFloat”不可转换为“ Double”

I get this error when I put i or j in the two variables. ij放在两个变量中时出现此错误。

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"

What is the problem? 问题是什么?

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: 您必须将索引显式转换为CGFloat

var intWidth = ( CGFloat(i) * (sizeOfImage.width/3.0))
var fltHeight = ( CGFloat(j) * (sizeOfImage.height/3.0))

As often happening in swift, the error message is misleading - it says: 经常迅速发生的错误消息是令人误解的-它说:

'CGFloat' is not convertible to 'Double' “ CGFloat”不可转换为“ Double”

whereas I'd expect: 而我期望:

'CGFloat' is not convertible to 'Int' “ CGFloat”不可转换为“ Int”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM