2017-07-04 142 views
0

簡短說明病情:無法檢查提交按鈕迅速

我正所謂UserValue一個值。這將來自一個響應。並且我有一個Confirm按鈕按下方法。在這種情況下,我每次需要檢查用戶輸入金額是否大於UserValue的金額。

但是過一段時間的值將是UserValue將nill.That時間不應該檢查whethere在申請文本中輸入金額大於UserValue

現在,這裏我的代碼:

@IBAction func confirmButnClicked(_ sender: Any) { 
print(UserValue) 
let Mvalue = Double((UserValue.formattedAmount())) 
        let stringValue = Int(Mvalue!) 

if doubleValue < stringValue { 
     DialogUtils.showMessageWithOk(controller: self, message: "Maximum Value is : \(UserValue)") 

    } 
} 

其工作很好,當我在UserValue有一些價值,但是當我在這裏得到nill價值的崩潰......我該怎麼處理這個問題:

let stringValue = Int(Mvalue!) // crash here 

提前致謝!

回答

1

你正在強力展開nil,由於它的崩潰。

檢查UserValue是否爲nil或不。如果沒有nil然後做比較

@IBAction func confirmButnClicked(_ sender: Any) { 
    print(UserValue) 

    if let UserValue = UserValue { 
     if let Mvalue = Double((UserValue.formattedAmount())) { 
      if let stringValue = Int(Mvalue) { 
      if doubleValue < stringValue { 
       DialogUtils.showMessageWithOk(controller: self, message: "Maximum Value is : \(UserValue)") 
      } 
      } 
     } 
    } 
} 
+0

條件綁定的初始值設定項必須有可選類型,而不是'String' –

+0

UserValue是可選值嗎?如果不是,則使其成爲可選 – Subramanian

+0

是的,某些時間值將會有一段時間vlaue不會來 –

0

相反的力鑄造MValue爲Int時MValue是零或除了整數

let stringValue = Int(Mvalue!) 

使用任何其它類型的內容,如下面

if let stringValue = Int(Mvalue) { 
    //Now it prints your perfect unwrapping value 
    if doubleValue < stringValue { 
     DialogUtils.showMessageWithOk(controller: self, message: "Maximum Value is : \(UserValue)") 
    } 
} 
你會得到錯誤