二进制运算符'<'不能应用于'Double'类型的操作数和& # 39;cgfloat



此代码无语法错误。

for (var m = 1.0; m < 3.0; m += 0.1) {
}
另一方面,下面的代码有一个语法错误。错误消息:二进制运算符'<'不能应用于'Double'和'CGFloat'类型的操作数
let image = UIImage(named: "myImage")
for (var n = 1.0; n < image!.size.height; n += 0.1) {
}

为什么会这样?我尝试使用if let而不是强制展开,但我有同样的错误。

环境:Xcode7.0.1Swift2

因为image!.size.height返回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) {
}

或者您可以这样将n的type赋值为CGFloat:

for (var n : CGFloat = 1.0; n < 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
}

相关内容

最新更新