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

我们两清 提交于 2019-12-02 04:17:27

问题


I'm trying to cut an image into 9 pieces in Swift. I'm 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"

What is the problem?


回答1:


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 CGFloats:

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'

whereas I'd expect:

'CGFloat' is not convertible to 'Int'



来源:https://stackoverflow.com/questions/26336231/cgfloat-is-not-convertible-to-double-error-in-swift-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!