Swift 二进制运算符'<';无法应用于类型为';双倍';和';CGFloat&x27;

Swift 二进制运算符'<';无法应用于类型为';双倍';和';CGFloat&x27;,swift,swift2,Swift,Swift2,此代码没有语法错误 for (var m = 1.0; m < 3.0; m += 0.1) { } for(var m=1.0;m

此代码没有语法错误

for (var m = 1.0; m < 3.0; m += 0.1) {
}
for(var m=1.0;m<3.0;m+=0.1){
}
另一方面,下面的代码有语法错误。
错误消息:二进制运算符',因为
image!。size.height
return
CGFloat
任何类型的
n
都是
Double
,因此您需要将
CGFloat
转换为
Double
这样
Double(image!.size.height)

您的代码将是:

let image = UIImage(named: "myImage")

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

}

实现这一点的一种稍微经典的方法是使用
stride()
函数系列

// Create the image, crashing if it doesn't exist. since the error case has been handled, there is no need to force unwrap the image anymore.
guard let image = UIImage(named: "myImage") else { fatalError() }

// The height parameter returns a CGFloat, convert it to a Double for consistency across platforms.
let imageHeight = Double(image.size.height)

// Double conforms to the `Strideable` protocol, so we can use the stride(to:by:) function to enumerate through a range with a defined step value.
for n in 1.0.stride(to: imageHeight, by: 0.1) {
    print("\(n)")
    // ... Or do whatever you want to in here.
}

请在Swift 4.0上查看此项

var enteringAmountDouble: Double? {
    return Double(amountTextField.text!)
}
var userMoneyDouble: Double? = userWalletMerchants?.balance
if (enteringAmountDouble?.isLessThanOrEqualTo(userMoneyDouble!))! {
    print("Balance is there .. U can transfer money to someone!")
}else{
    APIInterface.instance().showAlert(title: "Please check you are balance", message: "Insufficient balance")
    return
}

您是否检查了
的操作数类型谢谢您的建议。我没有得到证实。下次我会检查类型。哦,我误解了“1.0”是作为CGFloat构建的。我也误解了错误信息所说的。非常感谢你的回答。我没有确认。哇!这是最好的方式!!我不知道Streable协议,但主要类型,比如Double、Float、Int,符合这个。这很明显,我很兴奋!!谢谢你的建议!我用这个!
// Create the image, crashing if it doesn't exist. since the error case has been handled, there is no need to force unwrap the image anymore.
guard let image = UIImage(named: "myImage") else { fatalError() }

// The height parameter returns a CGFloat, convert it to a Double for consistency across platforms.
let imageHeight = Double(image.size.height)

// Double conforms to the `Strideable` protocol, so we can use the stride(to:by:) function to enumerate through a range with a defined step value.
for n in 1.0.stride(to: imageHeight, by: 0.1) {
    print("\(n)")
    // ... Or do whatever you want to in here.
}
var enteringAmountDouble: Double? {
    return Double(amountTextField.text!)
}
var userMoneyDouble: Double? = userWalletMerchants?.balance
if (enteringAmountDouble?.isLessThanOrEqualTo(userMoneyDouble!))! {
    print("Balance is there .. U can transfer money to someone!")
}else{
    APIInterface.instance().showAlert(title: "Please check you are balance", message: "Insufficient balance")
    return
}