Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swift2

This code has no syntax error.

for (var m = 1.0; m < 3.0; m += 0.1) {
}

On the other hand, the below code has an syntax error. Error Message: Binary operator '<' cannot be applied to operands of type 'Double' and 'CGFloat'

let image = UIImage(named: "myImage")
for (var n = 1.0; n < image!.size.height; n += 0.1) {
}

Why it happend? I tried to use if let instead of force unwrap, but I had the same error.

Environment: Xcode7.0.1 Swift2

like image 461
Maiko Ohkawa Avatar asked Dec 18 '22 23:12

Maiko Ohkawa


1 Answers

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) {

}
like image 54
Dharmesh Kheni Avatar answered May 29 '23 15:05

Dharmesh Kheni