Swift binary operator '+' cannot be applied to two CGFloat operands

37,880

Solution 1

The error message is wrong. The problem is that you are trying to multiply an Int and a CGFloat.

Replace:

innerY = innerY - CGFloat(gridHeight * row)

with:

innerY = innerY - gridHeight * CGFloat(row)

The answer above is for the current version of your code. For the commented out version that corresponds to the error message you posted:

Replace:

var innerY: CGFloat = CGFloat(relativePosition.y) + CGFloat(gridHeight * row)

with

var innerY: CGFloat = CGFloat(relativePosition.y) + gridHeight * CGFloat(row)

Solution 2

Looking through all the answers, castings, conversion and extensions, I think the best solution yet to say for Swift is Operator Overload. Code sample below for Swift 3.0:

/// overloads -/+ between a cgfloat on the left and an int/double on the right
func - (left: CGFloat, right: Double) -> CGFloat {
    return left - CGFloat(right);
}

func - (left: CGFloat, right: Int) -> CGFloat {
    return left - CGFloat(right);
}

func + (left: CGFloat, right: Double) -> CGFloat {
    return left + CGFloat(right);
}

func + (left: CGFloat, right: Int) -> CGFloat {
    return left + CGFloat(right);
}

Put this into a global place OUTSIDE of any class. See magic occurs.

Solution 3

Indeed, there is something else wrong, this works:

import QuartzCore

let a:CGFloat = 1
let b:CGFloat = 2

let c = a + b
var innerY: CGFloat = CGFloat(1.0) + CGFloat(2.0)

and CGFloat implements the FloatingPointType type

Share:
37,880
Colliot
Author by

Colliot

Just a young man. My Blog

Updated on July 09, 2022

Comments

  • Colliot
    Colliot almost 2 years

    I am writing a function in Swift to detect which hexagon I am clicking on. But I ran into a peculiar error message stating that I cannot add two CGFloats. Whatever I did, e.g. changing let to var, declare and assign separately, did not work. I guess there must be something else wrong, but I cannot find it. The code is as follows:

    func pointToCoordinate(position: CGPoint) -> (Int, Int) {
            var gridHeight = 2 * singleRadius / sqrt(CGFloat(3)) * 1.5
            var gridWidth = 2 * singleRadius
            var halfWidth = singleRadius
    
            var relativePosition = CGPoint(x: position.x - totalRadius / 2, y: position.y - totalRadius / 2)
            println((relativePosition.y / gridHeight))
            var row = -(cgfloatToInt)(relativePosition.y / gridHeight)
            var column: Int
    
            println((relativePosition.x + halfWidth * CGFloat(row + 1)) / (gridWidth))
            column = cgfloatToInt((relativePosition.x + halfWidth * CGFloat(row + 1)) / (gridWidth))
    
    //        var innerY: CGFloat = CGFloat(relativePosition.y) + CGFloat(gridHeight * row)
            var innerX = relativePosition.x
            var innerY = relativePosition.y
    
            innerY = innerY - CGFloat(gridHeight * row)
    
            println((innerX, innerY))
            return (column, row)
        }
    

    Error Message

  • Dan Rosenstark
    Dan Rosenstark over 7 years
    "The error message is wrong" is still very relevant regarding this same message in Xcode 8/Swift 3.
  • vacawama
    vacawama about 5 years
    @SpaceDog I find Swift compiler errors run the gamut from telling you what you need to know, accurate but only helpful to a compiler writer, and flat out wrong. This case was flat out wrong.
  • Duck
    Duck about 5 years
    @vacawama - I was by adding a CGFloat to a Float a few moments ago and the error message was "binary operator + cannot be applied to CGFloat" what is a bullcrap message. The correct message would have to be "you cannot add CGFloats and Floats". Binary operator is a confuse term because you think of binary operations. I create apps since 2008. I never saw a single Xcode error message pointing to the correct problem. They are all created to inflict maximum pain.