2017-12-01 4 views
-2

2つのInt値をどのように比較しますか?私はエラーを取得するInt値が他のInt値よりも大きいか等しいかどうかを確認しますか?値?

if (current >= self.limit) { 
      value = amount 
     } else { 
      value = current * 10 + amount 
      if value > self.max! { 
       value = amount 
      } 

    } 

Binary operator '>=' cannot be applied to operands of type 'Int' and 'Int?'

だろう何

let limit: Int? 
let current: Int = Int(self.stringValue)! 

しかし、私は(と以上)それらを比較してみてください:だから、私はこれを持っていますこれについて

+1

オプションをアンラップします。 – matt

答えて

2

IntInt?)のため、currentとは直接比較できません。したがって、最初にオプションをアンラップして、ハンドルなしのケースを検出して回避し、非ゼロのケースのみを比較します。

if let limit = self.limit, current >= limit { 
    value = amount 
} else { 
    value = current * 10 + amount 
    if value > self.max! { 
     value = amount 
    } 
} 
+0

それは簡単だったとは思えません。どうもありがとうございます! – HalesEnchanted

関連する問題