Binary Operator + Cannot Be Applied to Operands of Type Cgfloat Int

Binary operator * cannot be applied to operands of type Int and CGFloat

Instead of i * self.view.frame.size.width, use CGFloat(i) * self.view.frame.size.width

IOS Swift Binary operator '=' cannot be applied to operands of type 'CGFloat?' and 'Int' but '==' works

You misunderstood why the first case worked: 200 is a numeric literal, the compiler saw that you are checking this literal for equality against an Optional<CGFloat> so it interpreted 200 as a CGFloat. Since an Optional<CGFloat> is either nil or an actual number, you can test if it's equal to 200.

The second case is about ordering of the two values. The compiler does not have a rule about ranking nil against an actual value. You have to make that decision yourself.

In your case, I think this is what you actually want:

if let width = image?.size.width, width <= 200 {
// CODE
}

Binary operator '*' cannot be applied to operands of type 'CGRect' and 'CGFloat'

You should have stayed with 1 / 10, avoiding the cast. Changing that to CGFloat(1/10) actually introduces another problem, telling the compiler “hey, even though this is CGFloat parameter, I really want you to do integer math of 1 divided by 10 (which is zero!) and then cast that to a CGFloat.”

So, you can stick with 1 / 10 and because that parameter is CGFloat, it will correctly do the right floating point math. The real problem is simply that you were multiplying the view.frame times 1 / 10, rather than the view.frame.height:

scrollView.contentSize = CGSize(width: view.frame.width,
height: view.frame.height + (view.frame.height * 1 / 10 * CGFloat(UIImage.count)))

I don’t know what to make of that UIImage.count reference, so I’ve left that as is, but that’s obviously not right.


While the above works perfectly, but most of us, coming from other languages would explicitly specify the numbers as floating point numbers, say ... * 1.0 / 10.0 or just ... * 0.1 or whatever, to make it completely unambiguous that we don’t want it doing any integer math:

scrollView.contentSize = CGSize(width: view.frame.width,
height: view.frame.height + (view.frame.height / 10.0 * CGFloat(UIImage.count)))

This is, in this particular example, completely equivalent to what is above, but the use of 10.0 rather than 10 makes it absolutely clear that we want to do floating point math.

Binary operator '+' cannot be applied to two CGFloat operands?

It's absolutely possible, adding two CGFloat variables using the binary operator '+'. What you need to know is the resultant variable is also a CGFloat variable (based on type Inference Principle).

let value1 : CGFloat = 12.0
let value2 : CGFloat = 13.0
let value3 = value1 + value2
println("value3 \(value3)")

//The result is value3 25.0, and the value3 is of type CGFloat.

EDIT:
By Swift v3.0 convention

let value = CGFloat(12.0) + CGFloat(13.0)
println("value \(value)")

//The result is value 25.0, and the value is of type CGFloat.

Binary Operator + Cannot be Applied to Operands of Type CGfloat int

Declare them, instead, as the following:

let X : CGFloat = 0.0
let Y : CGFloat = 0.0

Replying to your comment:

The error has nothing to do with them being declared as var or let.
You could declare them as var and if you so insist on declaring them as Int, you would still need to do the following:

var X : Int = 0
var Y : Int = 0
ball.center = CGPointMake(view.center.x + CGFloat(X), view.center.y + CGFloat(Y))

Binary operator '/' cannot be applied to operands of type 'CGSize' and 'Int'

CGSize is a struct which contains two CGFloat values - width and height.

So you cannot divide it with an Int.

It seems that your parameter requires a CGFloat anyway. So you should be dividing the height or width by 2 and passing instead.

torpedoNode.physicsBody = SKPhysicsBody(circleOfRadius: torpedoNode.size.width / 2) //or height whichever is appropriate

Binary operator (*) cannot be applied to operands CGPoint and CGFloat

It is simply not possible to multiply a CGPoint with a Float. The first is a point and the second is a number. In real life, you can't multiply the coordinate where you're standing with a number. However what you can do, is to multiply each axis of the coordinate for itself and create a new point with the results.

I'm not sure what you're trying to do, but I'd simply change these lines to:

let gravityStep = CGPoint(x: 0, y: gravity * seconds)
let velocityStep = CGPoint(x: velocity.x * seconds, y: velocity.y * seconds)

This should work as expected.

Binary operator '' cannot be applied to operands of type 'Double' and 'CGFloat'

Because image!.size.height return CGFloat any type of your n is Double so you need to convert your CGFloat to Double this way Double(image!.size.height).

And your code will be:

let image = UIImage(named: "myImage")

for (var n = 1.0; n < Double(image!.size.height); n += 0.1) {

}

Or you can assign type to n as CGFloat this way:

for (var n : CGFloat = 1.0; n < image!.size.height; n += 0.1) {

}

Binary operator '..' cannot be applied to operands of type 'Int' and 'CGFloat'

You cannot create a CountableRange (or CountableClosedRange) with floating point types.

You either want to convert your 2 + self.frame.size.width / movingGroundTexture.size().width to an Int:

for i in 0 ..< Int(2 + self.frame.size.width / movingGroundTexture.size().width) { 
// i is an Int
}

Or you want to use stride (Swift 2 syntax):

for i in CGFloat(0).stride(to: 2 + self.frame.size.width / movingGroundTexture.size().width, by: 1) { 
// i is a CGFloat
}

Swift 3 syntax:

for i in stride(from: 0, to: 2 + self.frame.size.width / movingGroundTexture.size().width, by: 1) {
// i is a CGFloat
}

Depends on whether you need floating point precision or not. Note that if your upper bound is a non-integral value, the stride version will iterate one more time than the range operator version, due to the fact that Int(...) will ignore the fractional component.



Related Topics



Leave a reply



Submit