检查一个Int值是否大于或等于另一个Int?价值? [英] Check if an Int value is greater or equal to another Int? value?
本文介绍了检查一个Int值是否大于或等于另一个Int?价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您如何比较两个Int值?所以,我有这个:
How do you compare two Int values? So, I have this:
let limit: Int?
let current: Int = Int(self.stringValue)!
但是当我尝试比较它们时(大于或等于):
But when I try to compare them (greater or equal to):
if (current >= self.limit) {
value = amount
} else {
value = current * 10 + amount
if value > self.max! {
value = amount
}
}
我得到了错误:
二进制运算符'> ='不能应用于类型为'Int'的操作数,并且 整数?"
Binary operator '>=' cannot be applied to operands of type 'Int' and 'Int?'
如何解决这个问题?
推荐答案
由于limit
是可选的Int
(Int?
),它可能为零,不能与current
直接比较.因此,首先请打开可选选项以检测并避免处理零个案例,而仅比较非零个案例.
Because limit
is an optional Int
(Int?
) it may be nil and not directly comparable with current
. So first unwrap the optional to detect and avoid handle nil cases, and only compare non-nil cases.
if let limit = self.limit, current >= limit {
value = amount
} else {
value = current * 10 + amount
if value > self.max! {
value = amount
}
}
这篇关于检查一个Int值是否大于或等于另一个Int?价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文