简体   繁体   English

Swift-二进制运算符'> ='不能应用于类型为'String'和'Int'的操作数

[英]Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'

Not really understanding why this isn't working. 不太了解为什么这行不通。 I'm pretty new to the Swift world. 我是Swift世界的新手。

The error I'm getting is Binary operator '>=' cannot be applied to operands of type 'String' and 'Int' 我得到的错误是Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'

Could anyone help me understand why I'm getting this error? 谁能帮助我了解为什么我会收到此错误? Do I need to convert the String to a Double or is there something else I'm totally missing? 我需要将String转换为Double还是我完全缺少的其他东西? Again I'm new to Swift. 我还是Swift的新手。

Do I need to convert the String to a Double? 我需要将字符串转换为Double吗?

Yes, that's basically it. 是的,基本上就是这样。

You must declare first a variable to accumulate all the inputs: 您必须首先声明一个变量以累加所有输入:

var inputs = [Double]()

Observe that I'm declaring an array of Double because that's what we are interested in. 观察到我在声明一个Double数组,因为这是我们感兴趣的。

Then, each time you ask the input, convert the obtained String to Double and store it in your array: 然后,每次您询问输入时,都将获取的String转换为Double并将其存储在数组中:

print("Please enter a temperature\t", terminator: "")
var message : String = readLine()!
let value : Double = Double(message)!
inputs.append(value)

Finally, check all the accumulated values in inputs (you got this part right): 最后,检查inputs所有累加值(正确理解此部分):

for value in inputs {
    // value is already a Double
    if value >= 80 { 
        message = "hot!"
    }
    // etc.
}

I suggest researching how to convert to Double with error checking (ie how to detect "100 hot!" and ignore it because can't be converted). 我建议研究如何通过错误检查转换为Double (即,如何检测“ 100 hot!”并由于无法转换而将其忽略)。

Also, consider using a loop to read the values. 另外,考虑使用循环读取值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二元运算符不能应用于 int 和 int 类型的操作数? 斯威夫特 3 - Binary operator cannot be applied to operands of type int and int? Swift 3 Swift 3:二进制运算符不能应用于int和&#39;Int?&#39;类型的操作数 - Swift 3 : binary operator cannot be applied to operands of type int and 'Int?' 二进制运算符&#39;&gt; =&&lt;=&#39;不能应用于CGFloat和Int in Swift类型的操作数 - Binary Operator '>= & <=' cannot be applied to operands of type CGFloat and Int In Swift Swift:二进制运算符&#39;+&#39;不能应用于类型为&#39;AnyObject?&#39;的操作数 和&#39;Int&#39; - Swift: Binary operator '+' cannot be applied to operands of type 'AnyObject?' and 'Int' 二元运算符 &gt;=(和 &lt;)不能应用于 String 和 Int 类型的操作数 // Swift - Binary Operator >= (and <) cannot be applied to operands of types String and Int // Swift 二进制运算符不能应用于类型为Int和String的操作数-Swift 2.3-&gt; Swift 3.2转换错误 - Binary operator cannot be applied to operands of type Int and String - Swift 2.3 -> Swift 3.2 conversion error 二进制运算符-不能应用于字符串类型的操作数! 和诠释 - Binary operator - cannot be applied to operands of type String! and Int 二进制运算符&#39;==&#39;不能应用于&#39;Any?&#39;类型的操作数 和&#39;String&#39;Swift iOS - Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS 二进制运算符&#39;==&#39;不能应用于类型&#39;UIImage?&#39;的操作数 和“字符串”-Swift 4 - Binary operator '==' cannot be applied to operands of type 'UIImage?' and 'String' - Swift 4 Swift 二元运算符“+=”不能应用于“AnyObject!”类型的操作数和“字符串!” - Swift Binary operator '+=' cannot be applied to operands of type 'AnyObject!' and 'String!'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM